refactor(errors): add parsing error types for context info (#911)

This commit is contained in:
Sampras Lopes
2023-05-11 18:15:00 +05:30
committed by GitHub
parent f790099368
commit 0d46690502
9 changed files with 77 additions and 67 deletions

View File

@ -8,36 +8,23 @@
///
pub type CustomResult<T, E> = error_stack::Result<T, E>;
macro_rules! impl_error_display {
($st: ident, $arg: tt) => {
impl std::fmt::Display for $st {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.write_str(&format!(
"{{ error_type: {:?}, error_description: {} }}",
self, $arg
))
}
}
};
/// Parsing Errors
#[derive(Debug, thiserror::Error)]
pub enum ParsingError {
///Failed to parse enum
#[error("Failed to parse enum: {0}")]
EnumParseFailure(&'static str),
///Failed to parse struct
#[error("Failed to parse struct: {0}")]
StructParseFailure(&'static str),
/// Failed to encode data to given format
#[error("Failed to serialize to {0} format")]
EncodeError(&'static str),
/// Failed to parse data
#[error("Unknown error while parsing")]
UnknownError,
}
macro_rules! impl_error_type {
($name: ident, $arg: tt) => {
#[doc = ""]
#[doc = stringify!(Error variant $name)]
#[doc = stringify!(Custom error variant for $arg)]
#[doc = ""]
#[derive(Debug)]
pub struct $name;
impl_error_display!($name, $arg);
impl std::error::Error for $name {}
};
}
impl_error_type!(ParsingError, "Parsing error");
/// Validation errors.
#[allow(missing_docs)] // Only to prevent warnings about struct fields not being documented
#[derive(Debug, thiserror::Error)]