sentinel_dbms/
error.rs

1use thiserror::Error;
2
3/// Sentinel-wide error type for the document DBMS.
4///
5/// This error type encompasses all possible errors that can occur within
6/// the Sentinel system, providing structured error handling and meaningful
7/// error messages for different failure scenarios.
8#[derive(Error, Debug)]
9pub enum SentinelError {
10    /// I/O operations failed (file system, network, etc.)
11    #[error("I/O error: {source}")]
12    Io {
13        #[from]
14        source: std::io::Error,
15    },
16
17    /// JSON serialization/deserialization failed
18    #[error("JSON error: {source}")]
19    Json {
20        #[from]
21        source: serde_json::Error,
22    },
23
24    /// Document not found in collection
25    #[error("Document '{id}' not found in collection '{collection}'")]
26    DocumentNotFound {
27        id:         String,
28        collection: String,
29    },
30
31    /// Collection not found in store
32    #[error("Collection '{name}' not found in store")]
33    CollectionNotFound {
34        name: String,
35    },
36
37    /// Document already exists (for operations that require uniqueness)
38    #[error("Document '{id}' already exists in collection '{collection}'")]
39    DocumentAlreadyExists {
40        id:         String,
41        collection: String,
42    },
43
44    /// Invalid document ID format
45    #[error("Invalid document ID: {id}")]
46    InvalidDocumentId {
47        id: String,
48    },
49
50    /// Invalid collection name format
51    #[error("Invalid collection name: {name}")]
52    InvalidCollectionName {
53        name: String,
54    },
55
56    /// Store is corrupted or in an invalid state
57    #[error("Store corruption detected: {reason}")]
58    StoreCorruption {
59        reason: String,
60    },
61
62    /// Transaction failed
63    #[error("Transaction failed: {reason}")]
64    TransactionFailed {
65        reason: String,
66    },
67
68    /// Lock acquisition failed
69    #[error("Lock acquisition failed: {reason}")]
70    LockFailed {
71        reason: String,
72    },
73
74    /// Encryption/decryption operation failed
75    #[error("Cryptographic operation failed: {operation}")]
76    CryptoFailed {
77        operation: String,
78    },
79
80    /// Configuration error
81    #[error("Configuration error: {message}")]
82    ConfigError {
83        message: String,
84    },
85
86    /// Generic error for unexpected conditions
87    #[error("Internal error: {message}")]
88    Internal {
89        message: String,
90    },
91}
92
93/// Result type alias for Sentinel operations.
94pub type Result<T> = std::result::Result<T, SentinelError>;
95
96impl From<sentinel_crypto::CryptoError> for SentinelError {
97    fn from(err: sentinel_crypto::CryptoError) -> Self {
98        Self::CryptoFailed {
99            operation: err.to_string(),
100        }
101    }
102}
103
104#[cfg(test)]
105mod tests {
106    use super::*;
107
108    #[test]
109    fn test_sentinel_error_from_crypto_error() {
110        let crypto_err = sentinel_crypto::CryptoError::Encryption;
111        let sentinel_err: SentinelError = crypto_err.into();
112        match sentinel_err {
113            SentinelError::CryptoFailed {
114                operation,
115            } => {
116                assert!(!operation.is_empty());
117            },
118            _ => panic!("Expected CryptoFailed"),
119        }
120    }
121}