Skip to content

Commit f43cf9c

Browse files
authored
fix: rm redundant revert in error message (#14215)
1 parent 97ffdfa commit f43cf9c

File tree

1 file changed

+19
-5
lines changed
  • crates/rpc/rpc-eth-types/src/error

1 file changed

+19
-5
lines changed

crates/rpc/rpc-eth-types/src/error/mod.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::time::Duration;
88
use alloy_eips::BlockId;
99
use alloy_primitives::{Address, Bytes, U256};
1010
use alloy_rpc_types_eth::{error::EthRpcErrorCode, request::TransactionInputError, BlockError};
11-
use alloy_sol_types::decode_revert_reason;
11+
use alloy_sol_types::{ContractError, RevertReason};
1212
use reth_errors::RethError;
1313
use reth_primitives_traits::transaction::signed::RecoveryError;
1414
use reth_rpc_server_types::result::{
@@ -615,8 +615,14 @@ impl RevertError {
615615
impl std::fmt::Display for RevertError {
616616
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
617617
f.write_str("execution reverted")?;
618-
if let Some(reason) = self.output.as_ref().and_then(|bytes| decode_revert_reason(bytes)) {
619-
write!(f, ": {reason}")?;
618+
if let Some(reason) = self.output.as_ref().and_then(|out| RevertReason::decode(out)) {
619+
let error = reason.to_string();
620+
let mut error = error.as_str();
621+
if matches!(reason, RevertReason::ContractError(ContractError::Revert(_))) {
622+
// we strip redundant `revert: ` prefix from the revert reason
623+
error = error.trim_start_matches("revert: ");
624+
}
625+
write!(f, ": {error}")?;
620626
}
621627
Ok(())
622628
}
@@ -768,9 +774,9 @@ pub fn ensure_success(result: ExecutionResult) -> EthResult<Bytes> {
768774

769775
#[cfg(test)]
770776
mod tests {
771-
use revm_primitives::b256;
772-
773777
use super::*;
778+
use alloy_sol_types::{Revert, SolError};
779+
use revm_primitives::b256;
774780

775781
#[test]
776782
fn timed_out_error() {
@@ -805,4 +811,12 @@ mod tests {
805811
EthApiError::HeaderNotFound(BlockId::finalized()).into();
806812
assert_eq!(err.message(), "block not found: finalized");
807813
}
814+
815+
#[test]
816+
fn revert_err_display() {
817+
let revert = Revert::from("test_revert_reason");
818+
let err = RevertError::new(revert.abi_encode().into());
819+
let msg = err.to_string();
820+
assert_eq!(msg, "execution reverted: test_revert_reason");
821+
}
808822
}

0 commit comments

Comments
 (0)