From 7aedee601a6b4d80c7bee6400834dda79be98e5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien?= Date: Sat, 11 Feb 2023 22:40:45 +0100 Subject: [PATCH] feat: convert EthApiError to RpcError directly (#1241) Co-authored-by: Georgios Konstantopoulos --- crates/rpc/rpc/src/eth/api/server.rs | 4 ++-- crates/rpc/rpc/src/eth/error.rs | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/crates/rpc/rpc/src/eth/api/server.rs b/crates/rpc/rpc/src/eth/api/server.rs index f5b04986a7..13a4845683 100644 --- a/crates/rpc/rpc/src/eth/api/server.rs +++ b/crates/rpc/rpc/src/eth/api/server.rs @@ -151,7 +151,7 @@ where } async fn get_code(&self, address: Address, block_number: Option) -> Result { - EthApi::get_code(self, address, block_number).to_rpc_result() + Ok(EthApi::get_code(self, address, block_number)?) } async fn call(&self, _request: CallRequest, _block_number: Option) -> Result { @@ -216,7 +216,7 @@ where } async fn send_raw_transaction(&self, tx: Bytes) -> Result { - EthApi::send_raw_transaction(self, tx).await.to_rpc_result() + Ok(EthApi::send_raw_transaction(self, tx).await?) } async fn sign(&self, _address: Address, _message: Bytes) -> Result { diff --git a/crates/rpc/rpc/src/eth/error.rs b/crates/rpc/rpc/src/eth/error.rs index ea758cf11d..dfde5fcf9e 100644 --- a/crates/rpc/rpc/src/eth/error.rs +++ b/crates/rpc/rpc/src/eth/error.rs @@ -1,8 +1,10 @@ //! Error variants for the `eth_` namespace. -use crate::{impl_to_rpc_result, result::ToRpcResult}; +use jsonrpsee::{core::Error as RpcError, types::error::INVALID_PARAMS_CODE}; use reth_transaction_pool::error::PoolError; +use crate::result::{internal_rpc_err, rpc_err}; + /// Result alias pub(crate) type EthResult = Result; @@ -27,7 +29,16 @@ pub(crate) enum EthApiError { Internal(#[from] reth_interfaces::Error), } -impl_to_rpc_result!(EthApiError); +impl From for RpcError { + fn from(value: EthApiError) -> Self { + match value { + EthApiError::UnknownBlockNumber => { + rpc_err(INVALID_PARAMS_CODE, value.to_string(), None) + } + err => internal_rpc_err(err.to_string()), + } + } +} /// A helper error type that mirrors `geth` Txpool's error messages #[derive(Debug, thiserror::Error)]