chainerrors_core/
decoder.rs1use crate::types::{DecodedError, ErrorContext};
4use thiserror::Error;
5
6#[derive(Debug, Error)]
8pub enum DecodeErrorError {
9 #[error("ABI decode failed: {reason}")]
10 AbiDecodeFailed { reason: String },
11
12 #[error("Invalid revert data: {reason}")]
13 InvalidData { reason: String },
14
15 #[error("Unsupported chain: {chain}")]
16 UnsupportedChain { chain: String },
17
18 #[error("Registry lookup failed: {reason}")]
19 RegistryError { reason: String },
20
21 #[error("{0}")]
22 Other(String),
23}
24
25pub trait ErrorDecoder: Send + Sync {
30 fn chain_family(&self) -> &'static str;
32
33 fn decode(
42 &self,
43 revert_data: &[u8],
44 ctx: Option<ErrorContext>,
45 ) -> Result<DecodedError, DecodeErrorError>;
46
47 fn decode_hex(
49 &self,
50 hex_str: &str,
51 ctx: Option<ErrorContext>,
52 ) -> Result<DecodedError, DecodeErrorError> {
53 let stripped = hex_str.strip_prefix("0x").unwrap_or(hex_str);
54 let bytes = hex::decode(stripped).map_err(|e| DecodeErrorError::InvalidData {
55 reason: format!("invalid hex: {e}"),
56 })?;
57 self.decode(&bytes, ctx)
58 }
59}