Skip to content

Commit edc42d2

Browse files
authored
chore: use thiserror for Error implementations (#127)
1 parent f2c02af commit edc42d2

2 files changed

Lines changed: 10 additions & 41 deletions

File tree

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ alloy-primitives = { version = "1.0", default-features = false, features = [
5252
alloy-rlp = { version = "0.3.9", default-features = false, features = [
5353
"derive",
5454
"arrayvec",
55+
"core-net",
56+
"core-error"
5557
] }
5658

5759
arrayvec = { version = "0.7", default-features = false }
@@ -62,6 +64,7 @@ derive_more = { version = "2", default-features = false, features = [
6264
"from",
6365
"not",
6466
] }
67+
thiserror = { version = "2", default-features = false }
6568
nybbles = { version = "0.4", default-features = false }
6669
smallvec = { version = "1.0", default-features = false, features = [
6770
"const_new",
@@ -94,6 +97,7 @@ std = [
9497
"arrayvec/std",
9598
"derive_more/std",
9699
"nybbles/std",
100+
"thiserror/std",
97101
"tracing/std",
98102
"serde?/std",
99103
]

src/proof/error.rs

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
use alloy_primitives::{B256, Bytes};
2-
use core::fmt;
32
use nybbles::Nibbles;
43

54
/// Error during proof verification.
6-
#[derive(PartialEq, Eq, Debug)]
5+
#[derive(PartialEq, Eq, Debug, thiserror::Error)]
76
pub enum ProofVerificationError {
87
/// State root does not match the expected.
8+
#[error("root mismatch. got: {got}. expected: {expected}")]
99
RootMismatch {
1010
/// Computed state root.
1111
got: B256,
1212
/// State root provided to verify function.
1313
expected: B256,
1414
},
1515
/// The node value does not match at specified path.
16+
#[error("value mismatch at path {path:?}. got: {got:?}. expected: {expected:?}")]
1617
ValueMismatch {
1718
/// Path at which error occurred.
1819
path: Nibbles,
@@ -22,45 +23,9 @@ pub enum ProofVerificationError {
2223
expected: Option<Bytes>,
2324
},
2425
/// Encountered unexpected empty root node.
26+
#[error("unexpected empty root node")]
2527
UnexpectedEmptyRoot,
2628
/// Error during RLP decoding of trie node.
27-
Rlp(alloy_rlp::Error),
28-
}
29-
30-
/// Enable Error trait implementation when core is stabilized.
31-
/// <https://github.com/rust-lang/rust/issues/103765>
32-
#[cfg(feature = "std")]
33-
impl std::error::Error for ProofVerificationError {
34-
fn source(&self) -> ::core::option::Option<&(dyn std::error::Error + 'static)> {
35-
#[allow(deprecated)]
36-
match self {
37-
Self::Rlp { 0: transparent } => {
38-
std::error::Error::source(transparent as &dyn std::error::Error)
39-
}
40-
_ => None,
41-
}
42-
}
43-
}
44-
45-
impl fmt::Display for ProofVerificationError {
46-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47-
match self {
48-
Self::RootMismatch { got, expected } => {
49-
write!(f, "root mismatch. got: {got}. expected: {expected}")
50-
}
51-
Self::ValueMismatch { path, got, expected } => {
52-
write!(f, "value mismatch at path {path:?}. got: {got:?}. expected: {expected:?}")
53-
}
54-
Self::UnexpectedEmptyRoot => {
55-
write!(f, "unexpected empty root node")
56-
}
57-
Self::Rlp(error) => fmt::Display::fmt(error, f),
58-
}
59-
}
60-
}
61-
62-
impl From<alloy_rlp::Error> for ProofVerificationError {
63-
fn from(source: alloy_rlp::Error) -> Self {
64-
Self::Rlp(source)
65-
}
29+
#[error(transparent)]
30+
Rlp(#[from] alloy_rlp::Error),
6631
}

0 commit comments

Comments
 (0)