alloy_dyn_abi/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use alloc::{borrow::Cow, string::String};
use alloy_primitives::{Selector, B256};
use alloy_sol_types::Error as SolTypesError;
use core::fmt;
use hex::FromHexError;
use parser::Error as TypeParserError;

/// Dynamic ABI result type.
pub type Result<T, E = Error> = core::result::Result<T, E>;

/// Error when parsing EIP-712 `encodeType` strings
///
/// <https://eips.ethereum.org/EIPS/eip-712#definition-of-encodetype>
#[derive(Clone, Debug, PartialEq)]
pub enum Error {
    /// Unknown type referenced from another type.
    #[cfg(feature = "eip712")]
    MissingType(String),
    /// Detected circular dep during typegraph resolution.
    #[cfg(feature = "eip712")]
    CircularDependency(String),
    /// Invalid property definition.
    #[cfg(feature = "eip712")]
    InvalidPropertyDefinition(String),

    /// Type mismatch during encoding or coercion.
    TypeMismatch {
        /// The expected type.
        expected: String,
        /// The actual type.
        actual: String,
    },
    /// Length mismatch during encoding.
    EncodeLengthMismatch {
        /// The expected length.
        expected: usize,
        /// The actual length.
        actual: usize,
    },

    /// Length mismatch during event topic decoding.
    TopicLengthMismatch {
        /// The expected length.
        expected: usize,
        /// The actual length.
        actual: usize,
    },

    /// Selector mismatch during function or error decoding.
    SelectorMismatch {
        /// The expected selector.
        expected: Selector,
        /// The actual selector.
        actual: Selector,
    },

    /// Invalid event signature.
    EventSignatureMismatch {
        /// The expected signature.
        expected: B256,
        /// The actual signature.
        actual: B256,
    },

    /// [`hex`] error.
    Hex(hex::FromHexError),
    /// [`alloy_sol_type_parser`] error.
    TypeParser(TypeParserError),
    /// [`alloy_sol_types`] error.
    SolTypes(SolTypesError),
}

impl From<FromHexError> for Error {
    #[inline]
    fn from(e: FromHexError) -> Self {
        Self::Hex(e)
    }
}

impl From<SolTypesError> for Error {
    #[inline]
    fn from(e: SolTypesError) -> Self {
        Self::SolTypes(e)
    }
}

impl From<TypeParserError> for Error {
    #[inline]
    fn from(e: TypeParserError) -> Self {
        Self::TypeParser(e)
    }
}

impl From<alloc::collections::TryReserveError> for Error {
    #[inline]
    fn from(value: alloc::collections::TryReserveError) -> Self {
        Self::SolTypes(value.into())
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
    #[inline]
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Hex(e) => Some(e),
            Self::TypeParser(e) => Some(e),
            Self::SolTypes(e) => Some(e),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            #[cfg(feature = "eip712")]
            Self::MissingType(name) => write!(f, "missing type in type resolution: {name}"),
            #[cfg(feature = "eip712")]
            Self::CircularDependency(dep) => write!(f, "circular dependency: {dep}"),
            #[cfg(feature = "eip712")]
            Self::InvalidPropertyDefinition(def) => write!(f, "invalid property definition: {def}"),

            Self::TypeMismatch { expected, actual } => write!(
                f,
                "type mismatch: expected type {expected:?}, got value with type {actual:?}",
            ),
            &Self::EncodeLengthMismatch { expected, actual } => {
                write!(f, "encode length mismatch: expected {expected} types, got {actual}",)
            }

            &Self::TopicLengthMismatch { expected, actual } => {
                write!(f, "invalid log topic list length: expected {expected} topics, got {actual}",)
            }
            Self::EventSignatureMismatch { expected, actual } => {
                write!(f, "invalid event signature: expected {expected}, got {actual}",)
            }
            Self::SelectorMismatch { expected, actual } => {
                write!(f, "selector mismatch: expected {expected}, got {actual}",)
            }
            Self::Hex(e) => e.fmt(f),
            Self::TypeParser(e) => e.fmt(f),
            Self::SolTypes(e) => e.fmt(f),
        }
    }
}

impl Error {
    /// Instantiates a new error with a static str.
    pub fn custom(s: impl Into<Cow<'static, str>>) -> Self {
        Self::SolTypes(SolTypesError::custom(s))
    }

    #[cfg(feature = "eip712")]
    pub(crate) fn eip712_coerce(expected: &crate::DynSolType, actual: &serde_json::Value) -> Self {
        #[allow(unused_imports)]
        use alloc::string::ToString;
        Self::TypeMismatch { expected: expected.to_string(), actual: actual.to_string() }
    }

    #[cfg(feature = "eip712")]
    pub(crate) fn invalid_property_def(def: &str) -> Self {
        Self::InvalidPropertyDefinition(def.into())
    }

    #[cfg(feature = "eip712")]
    pub(crate) fn missing_type(name: &str) -> Self {
        Self::MissingType(name.into())
    }

    #[cfg(feature = "eip712")]
    pub(crate) fn circular_dependency(dep: &str) -> Self {
        Self::CircularDependency(dep.into())
    }
}