refactor: replace OP error variant with general purpose error (#7844)

Co-authored-by: Oliver Nordbjerg <onbjerg@users.noreply.github.com>
This commit is contained in:
Darshan Kathiriya
2024-04-24 14:19:33 -04:00
committed by GitHub
parent 1f84c27c35
commit 784d8dc597
8 changed files with 31 additions and 27 deletions

View File

@@ -3,9 +3,10 @@
use jsonrpsee::types::ErrorObject; use jsonrpsee::types::ErrorObject;
use reqwest::Client; use reqwest::Client;
use reth_rpc::eth::{ use reth_rpc::eth::{
error::{EthApiError, EthResult, ToRpcError}, error::{EthApiError, EthResult},
traits::RawTransactionForwarder, traits::RawTransactionForwarder,
}; };
use reth_rpc_types::ToRpcError;
use std::sync::{atomic::AtomicUsize, Arc}; use std::sync::{atomic::AtomicUsize, Arc};
/// Error type when interacting with the Sequencer /// Error type when interacting with the Sequencer

View File

@@ -5,6 +5,7 @@ use reth_beacon_consensus::{BeaconForkChoiceUpdateError, BeaconOnNewPayloadError
use reth_engine_primitives::EngineObjectValidationError; use reth_engine_primitives::EngineObjectValidationError;
use reth_payload_builder::error::PayloadBuilderError; use reth_payload_builder::error::PayloadBuilderError;
use reth_primitives::{B256, U256}; use reth_primitives::{B256, U256};
use reth_rpc_types::ToRpcError;
use thiserror::Error; use thiserror::Error;
/// The Engine API result type /// The Engine API result type
@@ -86,11 +87,16 @@ pub enum EngineApiError {
/// The payload or attributes are known to be malformed before processing. /// The payload or attributes are known to be malformed before processing.
#[error(transparent)] #[error(transparent)]
EngineObjectValidationError(#[from] EngineObjectValidationError), EngineObjectValidationError(#[from] EngineObjectValidationError),
/// If the optimism feature flag is enabled, the payload attributes must have a present /// Any other error
/// gas limit for the forkchoice updated method. #[error("{0}")]
#[cfg(feature = "optimism")] Other(Box<dyn ToRpcError>),
#[error("Missing gas limit in payload attributes")] }
MissingGasLimitInPayloadAttributes,
impl EngineApiError {
/// Crates a new [EngineApiError::Other] variant.
pub fn other<E: ToRpcError>(err: E) -> Self {
Self::Other(Box::new(err))
}
} }
/// Helper type to represent the `error` field in the error response: /// Helper type to represent the `error` field in the error response:
@@ -188,15 +194,6 @@ impl From<EngineApiError> for jsonrpsee_types::error::ErrorObject<'static> {
) )
} }
}, },
// Optimism errors
#[cfg(feature = "optimism")]
EngineApiError::MissingGasLimitInPayloadAttributes => {
jsonrpsee_types::error::ErrorObject::owned(
INVALID_PARAMS_CODE,
INVALID_PARAMS_MSG,
Some(ErrorData::new(error)),
)
}
// Any other server error // Any other server error
EngineApiError::TerminalTD { .. } | EngineApiError::TerminalTD { .. } |
EngineApiError::TerminalBlockHash { .. } | EngineApiError::TerminalBlockHash { .. } |
@@ -206,6 +203,7 @@ impl From<EngineApiError> for jsonrpsee_types::error::ErrorObject<'static> {
SERVER_ERROR_MSG, SERVER_ERROR_MSG,
Some(ErrorData::new(error)), Some(ErrorData::new(error)),
), ),
EngineApiError::Other(err) => err.to_rpc_error(),
} }
} }
} }

View File

@@ -0,0 +1,9 @@
//! Implementation specific Errors for the `eth_` namespace.
use jsonrpsee_types::ErrorObject;
/// A tait to convert an error to an RPC error.
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>;
}

View File

@@ -1,5 +1,6 @@
//! Ethereum related types //! Ethereum related types
pub(crate) mod error;
pub mod transaction; pub mod transaction;
// re-export // re-export

View File

@@ -37,6 +37,7 @@ pub use eth::{
engine::{ engine::{
ExecutionPayload, ExecutionPayloadV1, ExecutionPayloadV2, ExecutionPayloadV3, PayloadError, ExecutionPayload, ExecutionPayloadV1, ExecutionPayloadV2, ExecutionPayloadV3, PayloadError,
}, },
error::ToRpcError,
transaction::{self, TransactionKind, TransactionRequest, TypedTransactionRequest}, transaction::{self, TransactionKind, TransactionRequest, TypedTransactionRequest},
}; };

View File

@@ -706,7 +706,7 @@ mod u256_numeric_string {
match val { match val {
serde_json::Value::String(s) => { serde_json::Value::String(s) => {
if let Ok(val) = s.parse::<u128>() { if let Ok(val) = s.parse::<u128>() {
return Ok(U256::from(val)) return Ok(U256::from(val));
} }
U256::from_str(&s).map_err(de::Error::custom) U256::from_str(&s).map_err(de::Error::custom)
} }

View File

@@ -6,7 +6,9 @@ use jsonrpsee::types::{error::CALL_EXECUTION_FAILED_CODE, ErrorObject};
use reth_interfaces::RethError; use reth_interfaces::RethError;
use reth_primitives::{revm_primitives::InvalidHeader, Address, Bytes, U256}; use reth_primitives::{revm_primitives::InvalidHeader, Address, Bytes, U256};
use reth_revm::tracing::{js::JsInspectorError, MuxError}; use reth_revm::tracing::{js::JsInspectorError, MuxError};
use reth_rpc_types::{error::EthRpcErrorCode, request::TransactionInputError, BlockError}; use reth_rpc_types::{
error::EthRpcErrorCode, request::TransactionInputError, BlockError, ToRpcError,
};
use reth_transaction_pool::error::{ use reth_transaction_pool::error::{
Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind, Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind,
PoolTransactionError, PoolTransactionError,
@@ -17,12 +19,6 @@ use std::time::Duration;
/// Result alias /// Result alias
pub type EthResult<T> = Result<T, EthApiError>; 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 /// Errors that can occur when interacting with the `eth_` namespace
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum EthApiError { pub enum EthApiError {

View File

@@ -1,11 +1,9 @@
//! Optimism specific types. //! Optimism specific types.
use jsonrpsee::types::ErrorObject; use jsonrpsee::types::ErrorObject;
use reth_rpc_types::ToRpcError;
use crate::{ use crate::{eth::error::EthApiError, result::internal_rpc_err};
eth::error::{EthApiError, ToRpcError},
result::internal_rpc_err,
};
/// Eth Optimism Api Error /// Eth Optimism Api Error
#[cfg(feature = "optimism")] #[cfg(feature = "optimism")]