migrate from anyhow to thiserror (#197)
* migrate from anyhow to thiserror (#190). pending polish error msgs * Add backtrace and compartmentalize errors - Include backtraces in the errors we generate. To get this we can't just return a literal enum, because the backtrace requires a call. - Related to the previous point: add methods to create errors so we can include the backtrace conveniently without changing too much the syntax. So instead of `Err(Error::KeyNotFound(key))` (literal enum) it will be `Err(Error::key_not_found(key))` (method call) - Each error should be local to its scope, and each scope should only return its own error. - The merkle tree should return `TreeError` and not Error - The middleware should return `MiddlewareError` and not Error - With a global Error we can't easily include backend/frontend types in the error fields, so declare a `BackendError` and a `FrontendError` and follow the pattern from the previous point - The Pod traits should be able to return backend errors and will be used in the frontend; for that we change them to use trait object Error: `dyn std::error::Error` * fix error * apply suggestions from @arnaucube * rename XError and XResult to Error and Result * reorg signature * make frontend custom error more ergonomic * remove unnecessary feature --------- Co-authored-by: Eduard S. <eduardsanou@posteo.net>
This commit is contained in:
parent
58d3c6a236
commit
29545f03fc
31 changed files with 696 additions and 273 deletions
63
src/frontend/error.rs
Normal file
63
src/frontend/error.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use std::{backtrace::Backtrace, fmt::Debug};
|
||||
|
||||
use crate::middleware::{DynError, Statement, StatementTmpl};
|
||||
|
||||
pub type Result<T, E = Error> = core::result::Result<T, E>;
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum InnerError {
|
||||
#[error("{0} {1} is over the limit {2}")]
|
||||
MaxLength(String, usize, usize),
|
||||
#[error("{0} doesn't match {1}")]
|
||||
StatementsDontMatch(Statement, StatementTmpl),
|
||||
#[error("invalid arguments to {0} operation")]
|
||||
OpInvalidArgs(String),
|
||||
// Other
|
||||
#[error("{0}")]
|
||||
Custom(String),
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("Inner: {inner}\n{backtrace}")]
|
||||
Inner {
|
||||
inner: Box<InnerError>,
|
||||
backtrace: Box<Backtrace>,
|
||||
},
|
||||
#[error(transparent)]
|
||||
Infallible(#[from] std::convert::Infallible),
|
||||
#[error(transparent)]
|
||||
Backend(#[from] Box<DynError>),
|
||||
#[error(transparent)]
|
||||
Middleware(#[from] crate::middleware::Error),
|
||||
}
|
||||
|
||||
impl Debug for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
std::fmt::Display::fmt(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! new {
|
||||
($inner:expr) => {
|
||||
Error::Inner {
|
||||
inner: Box::new($inner),
|
||||
backtrace: Box::new(Backtrace::capture()),
|
||||
}
|
||||
};
|
||||
}
|
||||
use InnerError::*;
|
||||
impl Error {
|
||||
pub(crate) fn custom(s: impl Into<String>) -> Self {
|
||||
new!(Custom(s.into()))
|
||||
}
|
||||
pub(crate) fn op_invalid_args(s: String) -> Self {
|
||||
new!(OpInvalidArgs(s))
|
||||
}
|
||||
pub(crate) fn statements_dont_match(s0: Statement, s1: StatementTmpl) -> Self {
|
||||
new!(StatementsDontMatch(s0, s1))
|
||||
}
|
||||
pub(crate) fn max_length(obj: String, found: usize, expect: usize) -> Self {
|
||||
new!(MaxLength(obj, found, expect))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue