use thiserror::Error;
#[derive(Debug, Clone, Error)]
pub enum StorageError {
#[error("invalid configuration: {0}")]
InvalidConfig(String),
#[error("invalid request: {0}")]
InvalidRequest(String),
#[error("database operation is not implemented yet: {note}")]
NotYetImplemented {
note: String,
},
#[error("unsupported capability `{capability}`{context}")]
UnsupportedCapability {
capability: String,
context: String,
},
#[error("database operation `{operation}` failed: {message}")]
BackendOperation {
operation: String,
message: String,
},
#[error("serialization error: {0}")]
Serialization(String),
#[error("io error: {0}")]
Io(String),
}
impl StorageError {
pub fn not_yet_implemented(note: impl Into<String>) -> Self {
Self::NotYetImplemented { note: note.into() }
}
pub fn unsupported_capability(
capability: impl Into<String>,
context: impl Into<String>,
) -> Self {
let context = context.into();
Self::UnsupportedCapability {
capability: capability.into(),
context: if context.is_empty() {
String::new()
} else {
format!(" ({context})")
},
}
}
pub fn backend_op(operation: impl Into<String>, message: impl Into<String>) -> Self {
Self::BackendOperation {
operation: operation.into(),
message: message.into(),
}
}
}
impl From<serde_json::Error> for StorageError {
fn from(value: serde_json::Error) -> Self {
Self::Serialization(value.to_string())
}
}
impl From<std::io::Error> for StorageError {
fn from(value: std::io::Error) -> Self {
Self::Io(value.to_string())
}
}