alloy_sol_types/
errors.rsuse crate::abi;
use alloc::{borrow::Cow, boxed::Box, collections::TryReserveError, string::String};
use alloy_primitives::{LogData, B256};
use core::fmt;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
TypeCheckFail {
expected_type: Cow<'static, str>,
data: String,
},
Overrun,
Reserve(TryReserveError),
BufferNotEmpty,
ReserMismatch,
RecursionLimitExceeded(u8),
InvalidEnumValue {
name: &'static str,
value: u8,
max: u8,
},
InvalidLog {
name: &'static str,
log: Box<LogData>,
},
UnknownSelector {
name: &'static str,
selector: alloy_primitives::FixedBytes<4>,
},
FromHexError(hex::FromHexError),
Other(Cow<'static, str>),
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Reserve(e) => Some(e),
Self::FromHexError(e) => Some(e),
_ => None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::TypeCheckFail { expected_type, data } => {
write!(f, "type check failed for {expected_type:?} with data: {data}",)
}
Self::Overrun => f.write_str("buffer overrun while deserializing"),
Self::Reserve(e) => e.fmt(f),
Self::BufferNotEmpty => f.write_str("buffer not empty after deserialization"),
Self::ReserMismatch => f.write_str("reserialization did not match original"),
Self::RecursionLimitExceeded(limit) => {
write!(f, "recursion limit of {limit} exceeded during decoding")
}
Self::InvalidEnumValue { name, value, max } => {
write!(f, "`{value}` is not a valid {name} enum value (max: `{max}`)")
}
Self::InvalidLog { name, log } => {
write!(f, "could not decode {name} from log: {log:?}")
}
Self::UnknownSelector { name, selector } => {
write!(f, "unknown selector `{selector}` for {name}")
}
Self::FromHexError(e) => e.fmt(f),
Self::Other(e) => f.write_str(e),
}
}
}
impl Error {
#[cold]
pub fn custom(s: impl Into<Cow<'static, str>>) -> Self {
Self::Other(s.into())
}
#[cold]
pub fn type_check_fail_sig(mut data: &[u8], signature: &'static str) -> Self {
if data.len() > 4 {
data = &data[..4];
}
let expected_type = signature.split('(').next().unwrap();
Self::type_check_fail(data, expected_type)
}
#[cold]
pub fn type_check_fail_token<T: crate::SolType>(token: &T::Token<'_>) -> Self {
Self::type_check_fail(&abi::encode(token), T::SOL_NAME)
}
#[cold]
pub fn type_check_fail(data: &[u8], expected_type: impl Into<Cow<'static, str>>) -> Self {
Self::TypeCheckFail { expected_type: expected_type.into(), data: hex::encode(data) }
}
#[cold]
pub fn unknown_selector(name: &'static str, selector: [u8; 4]) -> Self {
Self::UnknownSelector { name, selector: selector.into() }
}
#[doc(hidden)] #[cold]
pub fn invalid_event_signature_hash(name: &'static str, got: B256, expected: B256) -> Self {
Self::custom(format!(
"invalid signature hash for event {name:?}: got {got}, expected {expected}"
))
}
}
impl From<hex::FromHexError> for Error {
#[inline]
fn from(value: hex::FromHexError) -> Self {
Self::FromHexError(value)
}
}
impl From<TryReserveError> for Error {
#[inline]
fn from(value: TryReserveError) -> Self {
Self::Reserve(value)
}
}