use std::error::Error;
use transact::protos::ProtoConversionError;
#[derive(Debug)]
pub struct ScabbardClientError {
context: String,
source: Option<Box<dyn Error>>,
}
impl ScabbardClientError {
pub fn new(context: &str) -> Self {
Self {
context: context.into(),
source: None,
}
}
pub fn new_with_source(context: &str, err: Box<dyn Error>) -> Self {
Self {
context: context.into(),
source: Some(err),
}
}
}
impl Error for ScabbardClientError {}
impl std::fmt::Display for ScabbardClientError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if let Some(ref err) = self.source {
write!(f, "{}: {}", self.context, err)
} else {
f.write_str(&self.context)
}
}
}
impl From<ProtoConversionError> for ScabbardClientError {
fn from(err: ProtoConversionError) -> Self {
Self::new_with_source("protobuf conversion failed", err.into())
}
}