llm_optimizer_processor/storage/
error.rs1use thiserror::Error;
4
5pub type StorageResult<T> = Result<T, StorageError>;
7
8#[derive(Debug, Error)]
10pub enum StorageError {
11 #[error("Backend not available: {backend}")]
13 BackendNotAvailable { backend: String },
14
15 #[error("Connection error: {source}")]
17 ConnectionError {
18 #[source]
19 source: Box<dyn std::error::Error + Send + Sync>,
20 },
21
22 #[error("Query error: {message}")]
24 QueryError { message: String },
25
26 #[error("Serialization error: {source}")]
28 SerializationError {
29 #[source]
30 source: Box<dyn std::error::Error + Send + Sync>,
31 },
32
33 #[error("Deserialization error: {source}")]
35 DeserializationError {
36 #[source]
37 source: Box<dyn std::error::Error + Send + Sync>,
38 },
39
40 #[error("Transaction error: {message}")]
42 TransactionError { message: String },
43
44 #[error("Constraint violation: {message}")]
46 ConstraintViolation { message: String },
47
48 #[error("Not found: {entity} with key {key}")]
50 NotFound { entity: String, key: String },
51
52 #[error("Already exists: {entity} with key {key}")]
54 AlreadyExists { entity: String, key: String },
55
56 #[error("Invalid configuration: {message}")]
58 InvalidConfiguration { message: String },
59
60 #[error("Operation timed out after {duration_ms}ms")]
62 Timeout { duration_ms: u64 },
63
64 #[error("Invalid state: {message}")]
66 InvalidState { message: String },
67
68 #[error("Lock error: {message}")]
70 LockError { message: String },
71
72 #[error("Migration error: {message}")]
74 MigrationError { message: String },
75
76 #[error("Unsupported operation: {operation} not supported by {backend}")]
78 UnsupportedOperation { operation: String, backend: String },
79
80 #[error("Internal error: {message}")]
82 InternalError { message: String },
83
84 #[error("Capacity exceeded: {message}")]
86 CapacityError { message: String },
87
88 #[error("I/O error: {source}")]
90 IoError {
91 #[from]
92 source: std::io::Error,
93 },
94}
95
96impl StorageError {
97 pub fn is_retryable(&self) -> bool {
99 matches!(
100 self,
101 StorageError::ConnectionError { .. }
102 | StorageError::Timeout { .. }
103 | StorageError::LockError { .. }
104 )
105 }
106
107 pub fn is_transient(&self) -> bool {
109 matches!(
110 self,
111 StorageError::ConnectionError { .. }
112 | StorageError::Timeout { .. }
113 | StorageError::LockError { .. }
114 | StorageError::BackendNotAvailable { .. }
115 )
116 }
117
118 pub fn category(&self) -> ErrorCategory {
120 match self {
121 StorageError::BackendNotAvailable { .. } => ErrorCategory::Availability,
122 StorageError::ConnectionError { .. } => ErrorCategory::Connection,
123 StorageError::QueryError { .. } => ErrorCategory::Query,
124 StorageError::SerializationError { .. } | StorageError::DeserializationError { .. } => {
125 ErrorCategory::Serialization
126 }
127 StorageError::TransactionError { .. } => ErrorCategory::Transaction,
128 StorageError::ConstraintViolation { .. } => ErrorCategory::Constraint,
129 StorageError::NotFound { .. } => ErrorCategory::NotFound,
130 StorageError::AlreadyExists { .. } => ErrorCategory::Conflict,
131 StorageError::InvalidConfiguration { .. } => ErrorCategory::Configuration,
132 StorageError::Timeout { .. } => ErrorCategory::Timeout,
133 StorageError::InvalidState { .. } => ErrorCategory::State,
134 StorageError::LockError { .. } => ErrorCategory::Concurrency,
135 StorageError::MigrationError { .. } => ErrorCategory::Migration,
136 StorageError::UnsupportedOperation { .. } => ErrorCategory::Unsupported,
137 StorageError::InternalError { .. } => ErrorCategory::Internal,
138 StorageError::CapacityError { .. } => ErrorCategory::Capacity,
139 StorageError::IoError { .. } => ErrorCategory::Io,
140 }
141 }
142}
143
144#[derive(Debug, Clone, Copy, PartialEq, Eq)]
146pub enum ErrorCategory {
147 Availability,
148 Connection,
149 Query,
150 Serialization,
151 Transaction,
152 Constraint,
153 NotFound,
154 Conflict,
155 Configuration,
156 Timeout,
157 State,
158 Concurrency,
159 Migration,
160 Unsupported,
161 Internal,
162 Capacity,
163 Io,
164}
165
166impl ErrorCategory {
167 pub fn is_client_error(&self) -> bool {
169 matches!(
170 self,
171 ErrorCategory::Query
172 | ErrorCategory::Constraint
173 | ErrorCategory::NotFound
174 | ErrorCategory::Conflict
175 | ErrorCategory::Configuration
176 | ErrorCategory::State
177 | ErrorCategory::Unsupported
178 )
179 }
180
181 pub fn is_server_error(&self) -> bool {
183 matches!(
184 self,
185 ErrorCategory::Availability
186 | ErrorCategory::Connection
187 | ErrorCategory::Transaction
188 | ErrorCategory::Timeout
189 | ErrorCategory::Concurrency
190 | ErrorCategory::Migration
191 | ErrorCategory::Internal
192 | ErrorCategory::Capacity
193 | ErrorCategory::Io
194 )
195 }
196}