Skip to content

Commit

Permalink
feat(contract): improve 'no data' error message (#1898)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Jan 7, 2025
1 parent 1b0bb9a commit d809bd7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 14 additions & 0 deletions crates/contract/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub enum Error {
/// `contractAddress` was not found in the deployment transaction’s receipt.
#[error("missing `contractAddress` from deployment transaction receipt")]
ContractNotDeployed,
/// The contract returned no data.
#[error("contract call to `{0}` returned no data (\"0x\"); the called address might not be a contract")]
ZeroData(String, #[source] AbiError),
/// An error occurred ABI encoding or decoding.
#[error(transparent)]
AbiError(#[from] AbiError),
Expand All @@ -39,3 +42,14 @@ impl From<alloy_sol_types::Error> for Error {
Self::AbiError(e.into())
}
}

impl Error {
#[cold]
pub(crate) fn decode(name: &str, data: &[u8], error: AbiError) -> Self {
if data.is_empty() {
let name = name.split('(').next().unwrap_or(name);
return Self::ZeroData(name.to_string(), error);
}
Self::AbiError(error)
}
}
6 changes: 4 additions & 2 deletions crates/contract/src/eth_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ impl CallDecoder for Function {

#[inline]
fn abi_decode_output(&self, data: Bytes, validate: bool) -> Result<Self::CallOutput> {
FunctionExt::abi_decode_output(self, &data, validate).map_err(Error::AbiError)
FunctionExt::abi_decode_output(self, &data, validate)
.map_err(|e| Error::decode(&self.name, &data, e))
}

#[inline]
Expand All @@ -183,7 +184,8 @@ impl<C: SolCall> CallDecoder for PhantomData<C> {

#[inline]
fn abi_decode_output(&self, data: Bytes, validate: bool) -> Result<Self::CallOutput> {
C::abi_decode_returns(&data, validate).map_err(|e| Error::AbiError(e.into()))
C::abi_decode_returns(&data, validate)
.map_err(|e| Error::decode(C::SIGNATURE, &data, e.into()))
}

#[inline]
Expand Down

0 comments on commit d809bd7

Please sign in to comment.