reth_errors/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use reth_blockchain_tree_api::error::{BlockchainTreeError, CanonicalError};
use reth_consensus::ConsensusError;
use reth_execution_errors::BlockExecutionError;
use reth_fs_util::FsPathError;
use reth_storage_errors::{db::DatabaseError, provider::ProviderError};
use std::fmt::Display;

/// Result alias for [`RethError`].
pub type RethResult<T> = Result<T, RethError>;

/// Core error variants possible when interacting with the blockchain.
///
/// This enum encapsulates various error types that can occur during blockchain interactions.
///
/// It allows for structured error handling based on the nature of the encountered issue.
#[derive(Debug, thiserror::Error)]
pub enum RethError {
    /// Error encountered during block execution.
    #[error(transparent)]
    Execution(#[from] BlockExecutionError),

    /// Consensus-related errors.
    #[error(transparent)]
    Consensus(#[from] ConsensusError),

    /// Database-related errors.
    #[error(transparent)]
    Database(#[from] DatabaseError),

    /// Errors originating from providers.
    #[error(transparent)]
    Provider(#[from] ProviderError),

    /// Canonical errors encountered.
    #[error(transparent)]
    Canonical(#[from] CanonicalError),

    /// Any other error.
    #[error(transparent)]
    Other(Box<dyn core::error::Error + Send + Sync>),
}

impl RethError {
    /// Create a new `RethError` from a given error.
    pub fn other<E>(error: E) -> Self
    where
        E: core::error::Error + Send + Sync + 'static,
    {
        Self::Other(Box::new(error))
    }

    /// Create a new `RethError` from a given message.
    pub fn msg(msg: impl Display) -> Self {
        Self::Other(msg.to_string().into())
    }
}

impl From<BlockchainTreeError> for RethError {
    fn from(error: BlockchainTreeError) -> Self {
        Self::Canonical(CanonicalError::BlockchainTree(error))
    }
}

impl From<FsPathError> for RethError {
    fn from(err: FsPathError) -> Self {
        Self::other(err)
    }
}

// Some types are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
mod size_asserts {
    use super::*;

    macro_rules! static_assert_size {
        ($t:ty, $sz:expr) => {
            const _: [(); $sz] = [(); std::mem::size_of::<$t>()];
        };
    }

    static_assert_size!(RethError, 64);
    static_assert_size!(BlockExecutionError, 56);
    static_assert_size!(ConsensusError, 48);
    static_assert_size!(DatabaseError, 40);
    static_assert_size!(ProviderError, 48);
    static_assert_size!(CanonicalError, 56);
}