mod context;
pub use context::{Context, Location};
use crate::visitor::DecodeError;
use alloc::{borrow::Cow, boxed::Box, string::String, vec::Vec};
use core::fmt::Display;
#[derive(Debug)]
pub struct Error {
context: Context,
kind: ErrorKind,
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl Error {
pub fn new(kind: ErrorKind) -> Error {
Error { context: Context::new(), kind }
}
pub fn custom(error: impl CustomError) -> Error {
Error::new(ErrorKind::Custom(Box::new(error)))
}
pub fn custom_str(error: &'static str) -> Error {
#[derive(derive_more::Display, Debug)]
pub struct StrError(pub &'static str);
#[cfg(feature = "std")]
impl std::error::Error for StrError {}
Error::new(ErrorKind::Custom(Box::new(StrError(error))))
}
pub fn custom_string(error: String) -> Error {
#[derive(derive_more::Display, Debug)]
pub struct StringError(String);
#[cfg(feature = "std")]
impl std::error::Error for StringError {}
Error::new(ErrorKind::Custom(Box::new(StringError(error))))
}
pub fn kind(&self) -> &ErrorKind {
&self.kind
}
pub fn context(&self) -> &Context {
&self.context
}
pub fn at(mut self, loc: Location) -> Self {
self.context.push(loc);
Error { context: self.context, kind: self.kind }
}
pub fn at_idx(mut self, idx: usize) -> Self {
self.context.push(Location::idx(idx));
Error { context: self.context, kind: self.kind }
}
pub fn at_field(mut self, field: impl Into<Cow<'static, str>>) -> Self {
self.context.push(Location::field(field));
Error { context: self.context, kind: self.kind }
}
pub fn at_variant(mut self, variant: impl Into<Cow<'static, str>>) -> Self {
self.context.push(Location::variant(variant));
Error { context: self.context, kind: self.kind }
}
}
impl Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let path = self.context.path();
let kind = &self.kind;
write!(f, "Error at {path}: {kind}")
}
}
impl From<DecodeError> for Error {
fn from(err: DecodeError) -> Error {
Error::new(err.into())
}
}
impl From<codec::Error> for Error {
fn from(err: codec::Error) -> Error {
let err: DecodeError = err.into();
Error::new(err.into())
}
}
#[derive(Debug, derive_more::From, derive_more::Display)]
pub enum ErrorKind {
#[from]
#[display(fmt = "Error decoding bytes given the type ID and registry provided: {_0}")]
VisitorDecodeError(DecodeError),
#[display(fmt = "Number {value} is out of range")]
NumberOutOfRange {
value: String,
},
#[display(fmt = "Cannot find variant {got}; expects one of {expected:?}")]
CannotFindVariant {
got: String,
expected: Vec<&'static str>,
},
#[display(
fmt = "Cannot decode from type; expected length {expected_len} but got length {actual_len}"
)]
WrongLength {
actual_len: usize,
expected_len: usize,
},
#[display(fmt = "Field {name} does not exist in our encoded data")]
CannotFindField {
name: String,
},
#[from]
#[display(fmt = "Custom error: {_0}")]
Custom(Box<dyn CustomError>),
}
#[cfg(feature = "std")]
pub trait CustomError: std::error::Error + Send + Sync + 'static {}
#[cfg(feature = "std")]
impl<T: std::error::Error + Send + Sync + 'static> CustomError for T {}
#[cfg(not(feature = "std"))]
pub trait CustomError: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static {}
#[cfg(not(feature = "std"))]
impl<T: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static> CustomError for T {}
#[cfg(test)]
mod test {
use super::*;
#[derive(Debug, derive_more::Display)]
enum MyError {
Foo,
}
#[cfg(feature = "std")]
impl std::error::Error for MyError {}
#[test]
fn custom_error() {
Error::custom(MyError::Foo);
}
}