Skip to content

Commit

Permalink
feat(dyn-abi): improve hex error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jan 2, 2024
1 parent 49728f6 commit 33b2afe
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions crates/dyn-abi/src/coerce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ fn hex_error(input: &&str, e: FromHexError) -> ErrMode<ContextError> {
FromHexError::InvalidHexCharacter { .. } => unreachable!("{e:?}"),
FromHexError::InvalidStringLength | FromHexError::OddLength => ErrorKind::Eof,
};
ErrMode::from_external_error(input, kind, e)
ErrMode::from_external_error(input, kind, e).cut()
}

#[cfg(test)]
Expand All @@ -515,6 +515,14 @@ mod tests {
use alloy_primitives::address;
use core::str::FromStr;

#[track_caller]
fn assert_error_contains(e: &impl core::fmt::Display, s: &str) {
if cfg!(feature = "std") {
let es = e.to_string();
assert!(es.contains(s), "{s:?} not in {es:?}");
}
}

#[test]
fn coerce_bool() {
assert_eq!(DynSolType::Bool.coerce_str("true").unwrap(), DynSolValue::Bool(true));
Expand Down Expand Up @@ -840,10 +848,22 @@ mod tests {
DynSolValue::FixedBytes(mk_word(&[0x12, 0x34, 0x56]), 3)
);

assert!(DynSolType::FixedBytes(1).coerce_str("").is_err());
assert!(DynSolType::FixedBytes(1).coerce_str("0").is_err());
assert!(DynSolType::FixedBytes(1).coerce_str("0x").is_err());
assert!(DynSolType::FixedBytes(1).coerce_str("0x0").is_err());
let e = DynSolType::FixedBytes(1).coerce_str("").unwrap_err();
assert_error_contains(&e, "Invalid string length");
let e = DynSolType::FixedBytes(1).coerce_str("0").unwrap_err();
assert_error_contains(&e, "Odd number of digits");
let e = DynSolType::FixedBytes(1).coerce_str("0x").unwrap_err();
assert_error_contains(&e, "Invalid string length");
let e = DynSolType::FixedBytes(1).coerce_str("0x0").unwrap_err();
assert_error_contains(&e, "Odd number of digits");

let t = DynSolType::Array(Box::new(DynSolType::FixedBytes(1)));
let e = t.coerce_str("[0]").unwrap_err();
assert_error_contains(&e, "Odd number of digits");
let e = t.coerce_str("[0x]").unwrap_err();
assert_error_contains(&e, "Invalid string length");
let e = t.coerce_str("[0x0]").unwrap_err();
assert_error_contains(&e, "Odd number of digits");
}

#[test]
Expand Down

0 comments on commit 33b2afe

Please sign in to comment.