chore: add EthApiError::Other (#7398)

This commit is contained in:
Matthias Seitz
2024-03-30 15:03:46 +01:00
committed by GitHub
parent 16456ccc86
commit a81e7e41de
4 changed files with 69 additions and 36 deletions

View File

@ -52,7 +52,7 @@ use std::future::Future;
#[cfg(feature = "optimism")]
use crate::eth::api::optimism::OptimismTxMeta;
#[cfg(feature = "optimism")]
use crate::eth::error::OptimismEthApiError;
use crate::eth::optimism::OptimismEthApiError;
#[cfg(feature = "optimism")]
use reth_revm::optimism::RethL1BlockInfo;
#[cfg(feature = "optimism")]
@ -1321,10 +1321,10 @@ where
&envelope_buf,
tx.is_deposit(),
)
.map_err(|_| EthApiError::Optimism(OptimismEthApiError::L1BlockFeeError))?;
.map_err(|_| OptimismEthApiError::L1BlockFeeError)?;
let inner_l1_data_gas = l1_block_info
.l1_data_gas(&self.inner.provider.chain_spec(), block_timestamp, &envelope_buf)
.map_err(|_| EthApiError::Optimism(OptimismEthApiError::L1BlockGasError))?;
.map_err(|_| OptimismEthApiError::L1BlockGasError)?;
(Some(inner_l1_fee), Some(inner_l1_data_gas))
} else {
(None, None)
@ -1351,7 +1351,7 @@ where
target = "rpc::eth",
"Failed to serialize transaction for forwarding to sequencer"
);
EthApiError::Optimism(OptimismEthApiError::InvalidSequencerTransaction)
OptimismEthApiError::InvalidSequencerTransaction
})?;
self.inner
@ -1361,7 +1361,7 @@ where
.body(body)
.send()
.await
.map_err(|err| EthApiError::Optimism(OptimismEthApiError::HttpError(err)))?;
.map_err(OptimismEthApiError::HttpError)?;
}
Ok(())
}

View File

@ -20,6 +20,12 @@ use std::time::Duration;
/// Result alias
pub type EthResult<T> = Result<T, EthApiError>;
/// A tait for custom rpc errors used by [EthApiError::Other].
pub trait ToRpcError: std::error::Error + Send + Sync + 'static {
/// Converts the error to a JSON-RPC error object.
fn to_rpc_error(&self) -> ErrorObject<'static>;
}
/// Errors that can occur when interacting with the `eth_` namespace
#[derive(Debug, thiserror::Error)]
pub enum EthApiError {
@ -106,10 +112,6 @@ pub enum EthApiError {
#[error(transparent)]
/// Call Input error when both `data` and `input` fields are set and not equal.
TransactionInputError(#[from] TransactionInputError),
/// Optimism related error
#[error(transparent)]
#[cfg(feature = "optimism")]
Optimism(#[from] OptimismEthApiError),
/// Evm generic purpose error.
#[error("Revm error: {0}")]
EvmCustom(String),
@ -119,27 +121,16 @@ pub enum EthApiError {
/// Error thrown when tracing with a muxTracer fails
#[error(transparent)]
MuxTracerError(#[from] MuxError),
/// Any other error
#[error("0")]
Other(Box<dyn ToRpcError>),
}
/// Eth Optimism Api Error
#[cfg(feature = "optimism")]
#[derive(Debug, thiserror::Error)]
pub enum OptimismEthApiError {
/// Wrapper around a [hyper::Error].
#[error(transparent)]
HyperError(#[from] hyper::Error),
/// Wrapper around an [reqwest::Error].
#[error(transparent)]
HttpError(#[from] reqwest::Error),
/// Thrown when serializing transaction to forward to sequencer
#[error("invalid sequencer transaction")]
InvalidSequencerTransaction,
/// Thrown when calculating L1 gas fee
#[error("failed to calculate l1 gas fee")]
L1BlockFeeError,
/// Thrown when calculating L1 gas used
#[error("failed to calculate l1 gas used")]
L1BlockGasError,
impl EthApiError {
/// crates a new [EthApiError::Other] variant.
pub fn other<E: ToRpcError>(err: E) -> Self {
EthApiError::Other(Box::new(err))
}
}
impl From<EthApiError> for ErrorObject<'static> {
@ -178,14 +169,7 @@ impl From<EthApiError> for ErrorObject<'static> {
err @ EthApiError::InternalBlockingTaskError => internal_rpc_err(err.to_string()),
err @ EthApiError::InternalEthError => internal_rpc_err(err.to_string()),
err @ EthApiError::TransactionInputError(_) => invalid_params_rpc_err(err.to_string()),
#[cfg(feature = "optimism")]
EthApiError::Optimism(err) => match err {
OptimismEthApiError::HyperError(err) => internal_rpc_err(err.to_string()),
OptimismEthApiError::HttpError(err) => internal_rpc_err(err.to_string()),
OptimismEthApiError::InvalidSequencerTransaction |
OptimismEthApiError::L1BlockFeeError |
OptimismEthApiError::L1BlockGasError => internal_rpc_err(err.to_string()),
},
EthApiError::Other(err) => err.to_rpc_error(),
EthApiError::MuxTracerError(msg) => internal_rpc_err(msg.to_string()),
}
}

View File

@ -13,6 +13,9 @@ pub mod revm_utils;
mod signer;
pub(crate) mod utils;
#[cfg(feature = "optimism")]
pub mod optimism;
pub use api::{
fee_history::{fee_history_cache_new_blocks_task, FeeHistoryCache, FeeHistoryCacheConfig},
EthApi, EthApiSpec, EthTransactions, TransactionSource, RPC_DEFAULT_GAS_CAP,

View File

@ -0,0 +1,46 @@
//! Optimism specific types.
use crate::{
eth::error::{EthApiError, ToRpcError},
result::internal_rpc_err,
};
use jsonrpsee::types::ErrorObject;
/// Eth Optimism Api Error
#[cfg(feature = "optimism")]
#[derive(Debug, thiserror::Error)]
pub enum OptimismEthApiError {
/// Wrapper around a [hyper::Error].
#[error(transparent)]
HyperError(#[from] hyper::Error),
/// Wrapper around an [reqwest::Error].
#[error(transparent)]
HttpError(#[from] reqwest::Error),
/// Thrown when serializing transaction to forward to sequencer
#[error("invalid sequencer transaction")]
InvalidSequencerTransaction,
/// Thrown when calculating L1 gas fee
#[error("failed to calculate l1 gas fee")]
L1BlockFeeError,
/// Thrown when calculating L1 gas used
#[error("failed to calculate l1 gas used")]
L1BlockGasError,
}
impl ToRpcError for OptimismEthApiError {
fn to_rpc_error(&self) -> ErrorObject<'static> {
match self {
OptimismEthApiError::HyperError(err) => internal_rpc_err(err.to_string()),
OptimismEthApiError::HttpError(err) => internal_rpc_err(err.to_string()),
OptimismEthApiError::InvalidSequencerTransaction |
OptimismEthApiError::L1BlockFeeError |
OptimismEthApiError::L1BlockGasError => internal_rpc_err(self.to_string()),
}
}
}
impl From<OptimismEthApiError> for EthApiError {
fn from(err: OptimismEthApiError) -> Self {
EthApiError::other(err)
}
}