cfx_rpc_utils/error/
api.rs

1//! Helper traits to wrap generic l1 errors, in network specific error type
2//! configured in `reth_rpc_eth_api::EthApiTypes`.
3
4use crate::error::EthApiError;
5
6/// Helper trait to wrap core [`EthApiError`].
7pub trait FromEthApiError: From<EthApiError> {
8    /// Converts from error via [`EthApiError`].
9    fn from_eth_err<E>(err: E) -> Self
10    where EthApiError: From<E>;
11}
12
13impl<T> FromEthApiError for T
14where T: From<EthApiError>
15{
16    fn from_eth_err<E>(err: E) -> Self
17    where EthApiError: From<E> {
18        T::from(EthApiError::from(err))
19    }
20}
21
22/// Helper trait to wrap core [`EthApiError`].
23pub trait IntoEthApiError: Into<EthApiError> {
24    /// Converts into error via [`EthApiError`].
25    fn into_eth_err<E>(self) -> E
26    where E: FromEthApiError;
27}
28
29impl<T> IntoEthApiError for T
30where EthApiError: From<T>
31{
32    fn into_eth_err<E>(self) -> E
33    where E: FromEthApiError {
34        E::from_eth_err(self)
35    }
36}
37
38/// Helper trait to access wrapped core error.
39pub trait AsEthApiError {
40    /// Returns reference to [`EthApiError`], if this an error variant inherited
41    /// from core functionality.
42    fn as_err(&self) -> Option<&EthApiError>;
43
44    /// Returns `true` if error is
45    /// [`RpcInvalidTransactionError::GasTooHigh`].
46    fn is_gas_too_high(&self) -> bool {
47        if let Some(err) = self.as_err() {
48            return err.is_gas_too_high();
49        }
50
51        false
52    }
53
54    /// Returns `true` if error is
55    /// [`RpcInvalidTransactionError::GasTooLow`].
56    fn is_gas_too_low(&self) -> bool {
57        if let Some(err) = self.as_err() {
58            return err.is_gas_too_low();
59        }
60
61        false
62    }
63}
64
65impl AsEthApiError for EthApiError {
66    fn as_err(&self) -> Option<&EthApiError> { Some(self) }
67}