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

@ -5,7 +5,7 @@ use serde::{Deserialize, Serialize};
use crate::{
backends::plonky2::mock::{mainpod::MockMainPod, signedpod::MockSignedPod},
frontend::{MainPod, SignedPod, Statement},
frontend::{Error, MainPod, SignedPod, Statement},
middleware::{containers::Dictionary, Key, PodId, Value},
};
@ -20,14 +20,14 @@ pub struct SignedPodHelper {
}
impl TryFrom<SignedPodHelper> for SignedPod {
type Error = anyhow::Error;
type Error = Error;
fn try_from(helper: SignedPodHelper) -> Result<SignedPod, Self::Error> {
if helper.pod_class != "Signed" {
return Err(anyhow::anyhow!("pod_class is not Signed"));
return Err(Error::custom("pod_class is not Signed"));
}
if helper.pod_type != "Mock" {
return Err(anyhow::anyhow!("pod_type is not Mock"));
return Err(Error::custom("pod_type is not Mock"));
}
let dict = Dictionary::new(helper.entries.clone())?.clone();
@ -62,18 +62,18 @@ pub struct MainPodHelper {
}
impl TryFrom<MainPodHelper> for MainPod {
type Error = anyhow::Error; // or you can create a custom error type
type Error = Error; // or you can create a custom error type
fn try_from(helper: MainPodHelper) -> Result<Self, Self::Error> {
if helper.pod_class != "Main" {
return Err(anyhow::anyhow!("pod_class is not Main"));
return Err(Error::custom("pod_class is not Main"));
}
if helper.pod_type != "Mock" {
return Err(anyhow::anyhow!("pod_type is not Mock"));
return Err(Error::custom("pod_type is not Mock"));
}
let pod = MockMainPod::deserialize(helper.proof)
.map_err(|e| anyhow::anyhow!("Failed to deserialize proof: {}", e))?;
.map_err(|e| Error::custom(format!("Failed to deserialize proof: {}", e)))?;
Ok(MainPod {
pod: Box::new(pod),
@ -97,12 +97,10 @@ impl From<MainPod> for MainPodHelper {
mod tests {
use std::collections::HashSet;
use anyhow::Result;
// Pretty assertions give nicer diffs between expected and actual values
use pretty_assertions::assert_eq;
use schemars::schema_for;
// use schemars::generate::SchemaSettings;
use super::*;
use crate::{
backends::plonky2::mock::{mainpod::MockProver, signedpod::MockSigner},
@ -110,7 +108,7 @@ mod tests {
eth_dos_pod_builder, eth_friend_signed_pod_builder, zu_kyc_pod_builder,
zu_kyc_sign_pod_builders,
},
frontend::SignedPodBuilder,
frontend::{Result, SignedPodBuilder},
middleware::{
self,
containers::{Array, Set},