#[derive(Debug)]
pub struct EasyError {
source: Box<dyn std::error::Error + Send + Sync>,
context: String,
}
impl EasyError {
pub fn with_context<E>(source: E, context: &str) -> Self
where
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
EasyError {
source: source.into(),
context: context.to_string(),
}
}
pub fn source(&self) -> &(dyn std::error::Error + 'static) {
self.source.as_ref()
}
pub fn context(&self) -> &str {
&self.context
}
}
impl std::fmt::Display for EasyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.context, self.source)
}
}
impl std::error::Error for EasyError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&*self.source)
}
}