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:
arnaucube 2025-04-22 15:07:04 +02:00 committed by GitHub
parent 58d3c6a236
commit 29545f03fc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 696 additions and 273 deletions

View file

@ -1,6 +1,5 @@
use std::{fmt, iter, sync::Arc};
use anyhow::{anyhow, Result};
use log::error;
use plonky2::field::types::Field;
use serde::{Deserialize, Serialize};
@ -8,9 +7,9 @@ use serde::{Deserialize, Serialize};
use crate::{
backends::plonky2::primitives::merkletree::MerkleProof,
middleware::{
custom::KeyOrWildcard, AnchoredKey, CustomPredicateBatch, CustomPredicateRef,
NativePredicate, Params, Predicate, Statement, StatementArg, StatementTmplArg, ToFields,
Wildcard, WildcardValue, F, SELF,
custom::KeyOrWildcard, AnchoredKey, CustomPredicateBatch, CustomPredicateRef, Error,
NativePredicate, Params, Predicate, Result, Statement, StatementArg, StatementTmplArg,
ToFields, Wildcard, WildcardValue, F, SELF,
},
};
@ -253,11 +252,10 @@ impl Operation {
Self::ProductOf(s1, s2, s3)
}
(NO::MaxOf, (Some(s1), Some(s2), Some(s3)), OA::None, 3) => Self::MaxOf(s1, s2, s3),
_ => Err(anyhow!(
_ => Err(Error::custom(format!(
"Ill-formed operation {:?} with arguments {:?}.",
op_code,
args
))?,
op_code, args
)))?,
},
OperationType::Custom(cpr) => Self::Custom(cpr, args.to_vec()),
})
@ -320,10 +318,9 @@ impl Operation {
{
check_custom_pred(params, batch, *index, args, s_args)
}
_ => Err(anyhow!(
"Invalid deduction: {:?} ⇏ {:#}",
self,
output_statement
_ => Err(Error::invalid_deduction(
self.clone(),
output_statement.clone(),
)),
}
}
@ -386,17 +383,19 @@ fn check_custom_pred(
) -> Result<bool> {
let pred = &batch.predicates[index];
if pred.statements.len() != args.len() {
return Err(anyhow!(
"Custom predicate operation needs {} statements but has {}.",
return Err(Error::diff_amount(
"custom predicate operation".to_string(),
"statements".to_string(),
pred.statements.len(),
args.len()
args.len(),
));
}
if pred.args_len != s_args.len() {
return Err(anyhow!(
"Custom predicate statement needs {} args but has {}.",
return Err(Error::diff_amount(
"custom predicate statement".to_string(),
"args".to_string(),
pred.args_len,
s_args.len()
s_args.len(),
));
}