use thiserror::Error;
#[derive(Error, Debug)]
pub enum SentinelError {
#[error("I/O error: {source}")]
Io {
#[from]
source: std::io::Error,
},
#[error("JSON error: {source}")]
Json {
#[from]
source: serde_json::Error,
},
#[error("Document '{id}' not found in collection '{collection}'")]
DocumentNotFound {
id: String,
collection: String,
},
#[error("Collection '{name}' not found in store")]
CollectionNotFound {
name: String,
},
#[error("Document '{id}' already exists in collection '{collection}'")]
DocumentAlreadyExists {
id: String,
collection: String,
},
#[error("Invalid document ID: {id}")]
InvalidDocumentId {
id: String,
},
#[error("Invalid collection name: {name}")]
InvalidCollectionName {
name: String,
},
#[error("Store corruption detected: {reason}")]
StoreCorruption {
reason: String,
},
#[error("Transaction failed: {reason}")]
TransactionFailed {
reason: String,
},
#[error("Lock acquisition failed: {reason}")]
LockFailed {
reason: String,
},
#[error("Cryptographic operation failed: {operation}")]
CryptoFailed {
operation: String,
},
#[error("WAL error: {source}")]
Wal {
#[from]
source: sentinel_wal::WalError,
},
#[error("Configuration error: {message}")]
ConfigError {
message: String,
},
#[error("Document '{id}' hash verification failed: {reason}")]
HashVerificationFailed {
id: String,
reason: String,
},
#[error("Document '{id}' signature verification failed: {reason}")]
SignatureVerificationFailed {
id: String,
reason: String,
},
#[error("Internal error: {message}")]
Internal {
message: String,
},
}
pub type Result<T> = std::result::Result<T, SentinelError>;
impl From<sentinel_crypto::CryptoError> for SentinelError {
fn from(err: sentinel_crypto::CryptoError) -> Self {
Self::CryptoFailed {
operation: err.to_string(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sentinel_error_from_crypto_error() {
let crypto_err = sentinel_crypto::CryptoError::Encryption;
let sentinel_err: SentinelError = crypto_err.into();
match sentinel_err {
SentinelError::CryptoFailed {
operation,
} => {
assert!(!operation.is_empty());
},
_ => panic!("Expected CryptoFailed"),
}
}
}