use super::Severity;
use thiserror::Error;
#[derive(Error, Debug, PartialEq, Eq)]
pub enum StorageError {
#[error("Another instance of Kinetic daemon is already running (Database is locked).")]
DatabaseLocked,
#[error("Storage corruption detected: {0}")]
Corruption(String),
#[error("Storage operation failed: {0}")]
OperationFailed(String),
}
impl StorageError {
pub fn code(&self) -> &'static str {
match self {
Self::DatabaseLocked => "KIN-STO-001",
Self::Corruption(_) => "KIN-STO-002",
Self::OperationFailed(_) => "KIN-STO-003",
}
}
pub fn error_type_uri(&self) -> String {
format!("{}/errors/{}", crate::constants::DOCS_URL, self.code())
}
pub fn severity(&self) -> Severity {
match self {
Self::DatabaseLocked => Severity::Critical,
Self::Corruption(_) => Severity::Error,
Self::OperationFailed(_) => Severity::Error,
}
}
pub fn is_retryable(&self) -> bool {
matches!(self, Self::OperationFailed(_))
}
pub fn user_message(&self) -> String {
match self {
Self::DatabaseLocked => {
"Another instance of Kinetic daemon is already running (Database is locked)."
.to_string()
}
Self::Corruption(_) => {
"Storage corruption detected. The local database may need to be reset.".to_string()
}
Self::OperationFailed(_) => {
"A read or write operation failed on the local storage.".to_string()
}
}
}
}