reth_rpc_eth_types/error/
api.rs

1//! Helper traits to wrap generic l1 errors, in network specific error type configured in
2//! `reth_rpc_eth_api::EthApiTypes`.
3
4use crate::EthApiError;
5use reth_errors::ProviderError;
6use reth_evm::{ConfigureEvm, EvmErrorFor, HaltReasonFor};
7use revm::context_interface::result::HaltReason;
8
9use super::RpcInvalidTransactionError;
10
11/// Helper trait to wrap core [`EthApiError`].
12pub trait FromEthApiError: From<EthApiError> {
13    /// Converts from error via [`EthApiError`].
14    fn from_eth_err<E>(err: E) -> Self
15    where
16        EthApiError: From<E>;
17}
18
19impl<T> FromEthApiError for T
20where
21    T: From<EthApiError>,
22{
23    fn from_eth_err<E>(err: E) -> Self
24    where
25        EthApiError: From<E>,
26    {
27        T::from(EthApiError::from(err))
28    }
29}
30
31/// Helper trait to wrap core [`EthApiError`].
32pub trait IntoEthApiError: Into<EthApiError> {
33    /// Converts into error via [`EthApiError`].
34    fn into_eth_err<E>(self) -> E
35    where
36        E: FromEthApiError;
37}
38
39impl<T> IntoEthApiError for T
40where
41    EthApiError: From<T>,
42{
43    fn into_eth_err<E>(self) -> E
44    where
45        E: FromEthApiError,
46    {
47        E::from_eth_err(self)
48    }
49}
50
51/// Helper trait to access wrapped core error.
52pub trait AsEthApiError {
53    /// Returns reference to [`EthApiError`], if this an error variant inherited from core
54    /// functionality.
55    fn as_err(&self) -> Option<&EthApiError>;
56
57    /// Returns `true` if error is
58    /// [`RpcInvalidTransactionError::GasTooHigh`].
59    fn is_gas_too_high(&self) -> bool {
60        if let Some(err) = self.as_err() {
61            return err.is_gas_too_high()
62        }
63
64        false
65    }
66
67    /// Returns `true` if error is
68    /// [`RpcInvalidTransactionError::GasTooLow`].
69    fn is_gas_too_low(&self) -> bool {
70        if let Some(err) = self.as_err() {
71            return err.is_gas_too_low()
72        }
73
74        false
75    }
76}
77
78impl AsEthApiError for EthApiError {
79    fn as_err(&self) -> Option<&EthApiError> {
80        Some(self)
81    }
82}
83
84/// Helper trait to convert from revm errors.
85pub trait FromEvmError<Evm: ConfigureEvm>:
86    From<EvmErrorFor<Evm, ProviderError>> + FromEvmHalt<HaltReasonFor<Evm>>
87{
88    /// Converts from EVM error to this type.
89    fn from_evm_err(err: EvmErrorFor<Evm, ProviderError>) -> Self {
90        err.into()
91    }
92}
93
94impl<T, Evm> FromEvmError<Evm> for T
95where
96    T: From<EvmErrorFor<Evm, ProviderError>> + FromEvmHalt<HaltReasonFor<Evm>>,
97    Evm: ConfigureEvm,
98{
99}
100
101/// Helper trait to convert from revm errors.
102pub trait FromEvmHalt<Halt> {
103    /// Converts from EVM halt to this type.
104    fn from_evm_halt(halt: Halt, gas_limit: u64) -> Self;
105}
106
107impl FromEvmHalt<HaltReason> for EthApiError {
108    fn from_evm_halt(halt: HaltReason, gas_limit: u64) -> Self {
109        RpcInvalidTransactionError::halt(halt, gas_limit).into()
110    }
111}