Skip to main content

llm_optimizer_processor/storage/
error.rs

1//! Storage error types
2
3use thiserror::Error;
4
5/// Result type for storage operations
6pub type StorageResult<T> = Result<T, StorageError>;
7
8/// Errors that can occur during storage operations
9#[derive(Debug, Error)]
10pub enum StorageError {
11    /// Backend not available
12    #[error("Backend not available: {backend}")]
13    BackendNotAvailable { backend: String },
14
15    /// Connection error
16    #[error("Connection error: {source}")]
17    ConnectionError {
18        #[source]
19        source: Box<dyn std::error::Error + Send + Sync>,
20    },
21
22    /// Query error
23    #[error("Query error: {message}")]
24    QueryError { message: String },
25
26    /// Serialization error
27    #[error("Serialization error: {source}")]
28    SerializationError {
29        #[source]
30        source: Box<dyn std::error::Error + Send + Sync>,
31    },
32
33    /// Deserialization error
34    #[error("Deserialization error: {source}")]
35    DeserializationError {
36        #[source]
37        source: Box<dyn std::error::Error + Send + Sync>,
38    },
39
40    /// Transaction error
41    #[error("Transaction error: {message}")]
42    TransactionError { message: String },
43
44    /// Constraint violation
45    #[error("Constraint violation: {message}")]
46    ConstraintViolation { message: String },
47
48    /// Not found
49    #[error("Not found: {entity} with key {key}")]
50    NotFound { entity: String, key: String },
51
52    /// Already exists
53    #[error("Already exists: {entity} with key {key}")]
54    AlreadyExists { entity: String, key: String },
55
56    /// Invalid configuration
57    #[error("Invalid configuration: {message}")]
58    InvalidConfiguration { message: String },
59
60    /// Timeout
61    #[error("Operation timed out after {duration_ms}ms")]
62    Timeout { duration_ms: u64 },
63
64    /// Invalid state
65    #[error("Invalid state: {message}")]
66    InvalidState { message: String },
67
68    /// Lock error
69    #[error("Lock error: {message}")]
70    LockError { message: String },
71
72    /// Migration error
73    #[error("Migration error: {message}")]
74    MigrationError { message: String },
75
76    /// Unsupported operation
77    #[error("Unsupported operation: {operation} not supported by {backend}")]
78    UnsupportedOperation { operation: String, backend: String },
79
80    /// Internal error
81    #[error("Internal error: {message}")]
82    InternalError { message: String },
83
84    /// Capacity error
85    #[error("Capacity exceeded: {message}")]
86    CapacityError { message: String },
87
88    /// I/O error
89    #[error("I/O error: {source}")]
90    IoError {
91        #[from]
92        source: std::io::Error,
93    },
94}
95
96impl StorageError {
97    /// Check if error is retryable
98    pub fn is_retryable(&self) -> bool {
99        matches!(
100            self,
101            StorageError::ConnectionError { .. }
102                | StorageError::Timeout { .. }
103                | StorageError::LockError { .. }
104        )
105    }
106
107    /// Check if error is transient
108    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    /// Get error category
119    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/// Error category for classification
145#[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    /// Check if category represents a client error (4xx)
168    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    /// Check if category represents a server error (5xx)
182    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}