use thiserror::Error;
#[derive(Debug, Error)]
pub enum EngineError {
#[error("environment not open")]
EnvironmentNotOpen,
#[error("environment already open")]
EnvironmentAlreadyOpen,
#[error("environment closed")]
EnvironmentClosed,
#[error("environment failure: {0}")]
EnvironmentFailure(String),
#[error("invalid configuration: {0}")]
InvalidConfig(String),
#[error("database error: {0}")]
DatabaseError(String),
#[error("lock conflict: {0}")]
LockConflict(String),
#[error("deadlock detected")]
DeadlockDetected,
#[error("transaction error: {0}")]
TransactionError(String),
#[error("io error: {0}")]
IoError(#[from] std::io::Error),
#[error("dbi error: {0}")]
DbiError(#[from] noxu_dbi::DbiError),
#[error("evictor error: {0}")]
EvictorError(#[from] noxu_evictor::EvictorError),
#[error("cleaner error: {0}")]
CleanerError(#[from] noxu_cleaner::CleanerError),
#[error("recovery error: {0}")]
RecoveryError(#[from] noxu_recovery::RecoveryError),
}
pub type Result<T> = std::result::Result<T, EngineError>;
impl From<String> for EngineError {
fn from(msg: String) -> Self {
EngineError::DatabaseError(msg)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let err = EngineError::EnvironmentNotOpen;
assert_eq!(err.to_string(), "environment not open");
let err =
EngineError::EnvironmentFailure("corruption detected".to_string());
assert_eq!(err.to_string(), "environment failure: corruption detected");
let err =
EngineError::InvalidConfig("cache size too small".to_string());
assert_eq!(
err.to_string(),
"invalid configuration: cache size too small"
);
}
#[test]
fn test_error_from_io() {
let io_err =
std::io::Error::new(std::io::ErrorKind::NotFound, "file not found");
let err: EngineError = io_err.into();
assert!(matches!(err, EngineError::IoError(_)));
assert!(err.to_string().contains("file not found"));
}
#[test]
fn test_lock_errors() {
let err =
EngineError::LockConflict("timeout waiting for lock".to_string());
assert!(err.to_string().contains("lock conflict"));
let err = EngineError::DeadlockDetected;
assert_eq!(err.to_string(), "deadlock detected");
}
#[test]
fn test_transaction_error() {
let err =
EngineError::TransactionError("txn already aborted".to_string());
assert!(err.to_string().contains("transaction error"));
}
#[test]
fn test_database_error() {
let err = EngineError::DatabaseError("database not found".to_string());
assert!(err.to_string().contains("database error"));
}
}