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

@@ -5,6 +5,7 @@ use reth_beacon_consensus::{BeaconForkChoiceUpdateError, BeaconOnNewPayloadError
use reth_engine_primitives::EngineObjectValidationError;
use reth_payload_builder::error::PayloadBuilderError;
use reth_primitives::{B256, U256};
use reth_rpc_types::ToRpcError;
use thiserror::Error;
/// The Engine API result type
@@ -86,11 +87,16 @@ pub enum EngineApiError {
/// The payload or attributes are known to be malformed before processing.
#[error(transparent)]
EngineObjectValidationError(#[from] EngineObjectValidationError),
/// If the optimism feature flag is enabled, the payload attributes must have a present
/// gas limit for the forkchoice updated method.
#[cfg(feature = "optimism")]
#[error("Missing gas limit in payload attributes")]
MissingGasLimitInPayloadAttributes,
/// Any other error
#[error("{0}")]
Other(Box<dyn ToRpcError>),
}
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:
@@ -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
EngineApiError::TerminalTD { .. } |
EngineApiError::TerminalBlockHash { .. } |
@@ -206,6 +203,7 @@ impl From<EngineApiError> for jsonrpsee_types::error::ErrorObject<'static> {
SERVER_ERROR_MSG,
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
pub(crate) mod error;
pub mod transaction;
// re-export

View File

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

View File

@@ -706,7 +706,7 @@ mod u256_numeric_string {
match val {
serde_json::Value::String(s) => {
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)
}

View File

@@ -6,7 +6,9 @@ use jsonrpsee::types::{error::CALL_EXECUTION_FAILED_CODE, ErrorObject};
use reth_interfaces::RethError;
use reth_primitives::{revm_primitives::InvalidHeader, Address, Bytes, U256};
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::{
Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind,
PoolTransactionError,
@@ -17,12 +19,6 @@ 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 {

View File

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