reth_errors/
error.rs

1use reth_blockchain_tree_api::error::{BlockchainTreeError, CanonicalError};
2use reth_consensus::ConsensusError;
3use reth_execution_errors::BlockExecutionError;
4use reth_fs_util::FsPathError;
5use reth_storage_errors::{db::DatabaseError, provider::ProviderError};
6use std::fmt::Display;
7
8/// Result alias for [`RethError`].
9pub type RethResult<T> = Result<T, RethError>;
10
11/// Core error variants possible when interacting with the blockchain.
12///
13/// This enum encapsulates various error types that can occur during blockchain interactions.
14///
15/// It allows for structured error handling based on the nature of the encountered issue.
16#[derive(Debug, thiserror::Error)]
17pub enum RethError {
18    /// Error encountered during block execution.
19    #[error(transparent)]
20    Execution(#[from] BlockExecutionError),
21
22    /// Consensus-related errors.
23    #[error(transparent)]
24    Consensus(#[from] ConsensusError),
25
26    /// Database-related errors.
27    #[error(transparent)]
28    Database(#[from] DatabaseError),
29
30    /// Errors originating from providers.
31    #[error(transparent)]
32    Provider(#[from] ProviderError),
33
34    /// Canonical errors encountered.
35    #[error(transparent)]
36    Canonical(#[from] CanonicalError),
37
38    /// Any other error.
39    #[error(transparent)]
40    Other(Box<dyn core::error::Error + Send + Sync>),
41}
42
43impl RethError {
44    /// Create a new `RethError` from a given error.
45    pub fn other<E>(error: E) -> Self
46    where
47        E: core::error::Error + Send + Sync + 'static,
48    {
49        Self::Other(Box::new(error))
50    }
51
52    /// Create a new `RethError` from a given message.
53    pub fn msg(msg: impl Display) -> Self {
54        Self::Other(msg.to_string().into())
55    }
56}
57
58impl From<BlockchainTreeError> for RethError {
59    fn from(error: BlockchainTreeError) -> Self {
60        Self::Canonical(CanonicalError::BlockchainTree(error))
61    }
62}
63
64impl From<FsPathError> for RethError {
65    fn from(err: FsPathError) -> Self {
66        Self::other(err)
67    }
68}
69
70// Some types are used a lot. Make sure they don't unintentionally get bigger.
71#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
72mod size_asserts {
73    use super::*;
74
75    macro_rules! static_assert_size {
76        ($t:ty, $sz:expr) => {
77            const _: [(); $sz] = [(); std::mem::size_of::<$t>()];
78        };
79    }
80
81    static_assert_size!(RethError, 64);
82    static_assert_size!(BlockExecutionError, 56);
83    static_assert_size!(ConsensusError, 48);
84    static_assert_size!(DatabaseError, 32);
85    static_assert_size!(ProviderError, 48);
86    static_assert_size!(CanonicalError, 56);
87}