Skip to main content

aegis_common/
error.rs

1//! Aegis Error - Unified Error Types
2//!
3//! Comprehensive error handling for all Aegis operations. Categorizes errors
4//! by domain (storage, transaction, query, replication, auth) and provides
5//! utilities for determining retryability and error classification.
6//!
7//! Key Features:
8//! - Domain-specific error variants for precise error handling
9//! - Retryable error detection for automatic retry logic
10//! - User vs system error classification
11//! - Seamless integration with std::io::Error
12//!
13//! @version 0.1.0
14//! @author AutomataNexus Development Team
15
16use thiserror::Error;
17
18// =============================================================================
19// Error Types
20// =============================================================================
21
22/// Unified error type for all Aegis operations.
23#[derive(Error, Debug)]
24pub enum AegisError {
25    // Storage errors
26    #[error("storage error: {0}")]
27    Storage(String),
28
29    #[error("block not found: {0}")]
30    BlockNotFound(u64),
31
32    #[error("page not found: {0}")]
33    PageNotFound(u64),
34
35    #[error("corruption detected: {0}")]
36    Corruption(String),
37
38    // Transaction errors
39    #[error("transaction error: {0}")]
40    Transaction(String),
41
42    #[error("transaction aborted: {0}")]
43    TransactionAborted(String),
44
45    #[error("deadlock detected")]
46    Deadlock,
47
48    #[error("lock timeout")]
49    LockTimeout,
50
51    #[error("serialization failure")]
52    SerializationFailure,
53
54    // Query errors
55    #[error("parse error: {0}")]
56    Parse(String),
57
58    #[error("type error: {0}")]
59    TypeError(String),
60
61    #[error("execution error: {0}")]
62    Execution(String),
63
64    #[error("table not found: {0}")]
65    TableNotFound(String),
66
67    #[error("column not found: {0}")]
68    ColumnNotFound(String),
69
70    #[error("index not found: {0}")]
71    IndexNotFound(String),
72
73    // Constraint errors
74    #[error("constraint violation: {0}")]
75    ConstraintViolation(String),
76
77    #[error("unique constraint violation: {0}")]
78    UniqueViolation(String),
79
80    #[error("foreign key violation: {0}")]
81    ForeignKeyViolation(String),
82
83    #[error("check constraint violation: {0}")]
84    CheckViolation(String),
85
86    // Replication errors
87    #[error("replication error: {0}")]
88    Replication(String),
89
90    #[error("node not found: {0}")]
91    NodeNotFound(String),
92
93    #[error("quorum not reached")]
94    QuorumNotReached,
95
96    #[error("leader not elected")]
97    LeaderNotElected,
98
99    // Authentication/Authorization errors
100    #[error("authentication failed: {0}")]
101    AuthenticationFailed(String),
102
103    #[error("authorization denied: {0}")]
104    AuthorizationDenied(String),
105
106    // IO errors
107    #[error("IO error: {0}")]
108    Io(#[from] std::io::Error),
109
110    // Serialization errors
111    #[error("serialization error: {0}")]
112    Serialization(String),
113
114    // Encryption errors
115    #[error("encryption error: {0}")]
116    Encryption(String),
117
118    // Configuration errors
119    #[error("configuration error: {0}")]
120    Configuration(String),
121
122    // Internal errors
123    #[error("internal error: {0}")]
124    Internal(String),
125
126    // Resource errors
127    #[error("resource exhausted: {0}")]
128    ResourceExhausted(String),
129
130    #[error("memory limit exceeded")]
131    MemoryLimitExceeded,
132
133    // Network errors
134    #[error("network error: {0}")]
135    Network(String),
136
137    #[error("connection refused: {0}")]
138    ConnectionRefused(String),
139
140    #[error("timeout: {0}")]
141    Timeout(String),
142
143    // Vault errors
144    #[error("vault error: {0}")]
145    Vault(String),
146
147    // Shield errors
148    #[error("shield error: {0}")]
149    Shield(String),
150}
151
152// =============================================================================
153// Type Aliases
154// =============================================================================
155
156/// Result type alias for Aegis operations.
157pub type Result<T> = std::result::Result<T, AegisError>;
158
159// =============================================================================
160// Error Classification
161// =============================================================================
162
163impl AegisError {
164    /// Returns true if the operation can be safely retried.
165    pub fn is_retryable(&self) -> bool {
166        matches!(
167            self,
168            AegisError::Deadlock
169                | AegisError::LockTimeout
170                | AegisError::SerializationFailure
171                | AegisError::QuorumNotReached
172                | AegisError::LeaderNotElected
173                | AegisError::Network(_)
174                | AegisError::Timeout(_)
175                | AegisError::TransactionAborted(_)
176                | AegisError::Replication(_)
177                | AegisError::NodeNotFound(_)
178                | AegisError::ConnectionRefused(_)
179                | AegisError::ResourceExhausted(_)
180                | AegisError::MemoryLimitExceeded
181                | AegisError::Io(_)
182        )
183    }
184
185    /// Returns true if this is a user error (vs system error).
186    pub fn is_user_error(&self) -> bool {
187        matches!(
188            self,
189            AegisError::Parse(_)
190                | AegisError::TypeError(_)
191                | AegisError::TableNotFound(_)
192                | AegisError::ColumnNotFound(_)
193                | AegisError::ConstraintViolation(_)
194                | AegisError::UniqueViolation(_)
195                | AegisError::ForeignKeyViolation(_)
196                | AegisError::CheckViolation(_)
197                | AegisError::AuthenticationFailed(_)
198                | AegisError::AuthorizationDenied(_)
199                | AegisError::Execution(_)
200                | AegisError::IndexNotFound(_)
201                | AegisError::BlockNotFound(_)
202                | AegisError::PageNotFound(_)
203                | AegisError::Configuration(_)
204        )
205    }
206
207    /// Returns true if this is a system/internal error.
208    pub fn is_system_error(&self) -> bool {
209        matches!(
210            self,
211            AegisError::Storage(_)
212                | AegisError::Corruption(_)
213                | AegisError::Internal(_)
214                | AegisError::Encryption(_)
215                | AegisError::Serialization(_)
216        )
217    }
218
219    /// Returns true if this is a constraint violation error.
220    pub fn is_constraint_error(&self) -> bool {
221        matches!(
222            self,
223            AegisError::ConstraintViolation(_)
224                | AegisError::UniqueViolation(_)
225                | AegisError::ForeignKeyViolation(_)
226                | AegisError::CheckViolation(_)
227        )
228    }
229}