use std::fmt;
#[derive(Debug)]
pub struct StorageError {
message: String,
source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl StorageError {
pub fn new(message: impl Into<String>) -> Self {
Self {
message: message.into(),
source: None,
}
}
pub fn with_source(
message: impl Into<String>,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
Self {
message: message.into(),
source: Some(Box::new(source)),
}
}
}
impl fmt::Display for StorageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "state storage error: {}", self.message)?;
if let Some(ref src) = self.source {
write!(f, ": {src}")?;
}
Ok(())
}
}
impl std::error::Error for StorageError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source.as_ref().map(|e| e.as_ref() as _)
}
}