Skip to main content

noxu_engine/
error.rs

1//! Error types for the Noxu DB engine.
2
3use thiserror::Error;
4
5/// Errors that can occur during engine operations.
6#[derive(Debug, Error)]
7pub enum EngineError {
8    /// Environment is not open.
9    #[error("environment not open")]
10    EnvironmentNotOpen,
11
12    /// Environment is already open.
13    #[error("environment already open")]
14    EnvironmentAlreadyOpen,
15
16    /// Environment has been closed.
17    #[error("environment closed")]
18    EnvironmentClosed,
19
20    /// Environment has failed and is invalid.
21    #[error("environment failure: {0}")]
22    EnvironmentFailure(String),
23
24    /// Invalid configuration parameter.
25    #[error("invalid configuration: {0}")]
26    InvalidConfig(String),
27
28    /// Database operation error.
29    #[error("database error: {0}")]
30    DatabaseError(String),
31
32    /// Lock conflict occurred.
33    #[error("lock conflict: {0}")]
34    LockConflict(String),
35
36    /// Deadlock was detected.
37    #[error("deadlock detected")]
38    DeadlockDetected,
39
40    /// Transaction error.
41    #[error("transaction error: {0}")]
42    TransactionError(String),
43
44    /// I/O error occurred.
45    #[error("io error: {0}")]
46    IoError(#[from] std::io::Error),
47
48    /// Error from the DBI layer.
49    #[error("dbi error: {0}")]
50    DbiError(#[from] noxu_dbi::DbiError),
51
52    /// Error from the evictor.
53    #[error("evictor error: {0}")]
54    EvictorError(#[from] noxu_evictor::EvictorError),
55
56    /// Error from the cleaner.
57    #[error("cleaner error: {0}")]
58    CleanerError(#[from] noxu_cleaner::CleanerError),
59
60    /// Error from the recovery subsystem.
61    #[error("recovery error: {0}")]
62    RecoveryError(#[from] noxu_recovery::RecoveryError),
63}
64
65/// Result type for engine operations.
66pub type Result<T> = std::result::Result<T, EngineError>;
67
68impl From<String> for EngineError {
69    fn from(msg: String) -> Self {
70        EngineError::DatabaseError(msg)
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_error_display() {
80        let err = EngineError::EnvironmentNotOpen;
81        assert_eq!(err.to_string(), "environment not open");
82
83        let err =
84            EngineError::EnvironmentFailure("corruption detected".to_string());
85        assert_eq!(err.to_string(), "environment failure: corruption detected");
86
87        let err =
88            EngineError::InvalidConfig("cache size too small".to_string());
89        assert_eq!(
90            err.to_string(),
91            "invalid configuration: cache size too small"
92        );
93    }
94
95    #[test]
96    fn test_error_from_io() {
97        let io_err =
98            std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
99        let err: EngineError = io_err.into();
100        assert!(matches!(err, EngineError::IoError(_)));
101        assert!(err.to_string().contains("file not found"));
102    }
103
104    #[test]
105    fn test_lock_errors() {
106        let err =
107            EngineError::LockConflict("timeout waiting for lock".to_string());
108        assert!(err.to_string().contains("lock conflict"));
109
110        let err = EngineError::DeadlockDetected;
111        assert_eq!(err.to_string(), "deadlock detected");
112    }
113
114    #[test]
115    fn test_transaction_error() {
116        let err =
117            EngineError::TransactionError("txn already aborted".to_string());
118        assert!(err.to_string().contains("transaction error"));
119    }
120
121    #[test]
122    fn test_database_error() {
123        let err = EngineError::DatabaseError("database not found".to_string());
124        assert!(err.to_string().contains("database error"));
125    }
126}