use crate::types::{DecodedError, ErrorContext};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum DecodeErrorError {
#[error("ABI decode failed: {reason}")]
AbiDecodeFailed { reason: String },
#[error("Invalid revert data: {reason}")]
InvalidData { reason: String },
#[error("Unsupported chain: {chain}")]
UnsupportedChain { chain: String },
#[error("Registry lookup failed: {reason}")]
RegistryError { reason: String },
#[error("{0}")]
Other(String),
}
pub trait ErrorDecoder: Send + Sync {
fn chain_family(&self) -> &'static str;
fn decode(
&self,
revert_data: &[u8],
ctx: Option<ErrorContext>,
) -> Result<DecodedError, DecodeErrorError>;
fn decode_hex(
&self,
hex_str: &str,
ctx: Option<ErrorContext>,
) -> Result<DecodedError, DecodeErrorError> {
let stripped = hex_str.strip_prefix("0x").unwrap_or(hex_str);
let bytes = hex::decode(stripped).map_err(|e| DecodeErrorError::InvalidData {
reason: format!("invalid hex: {e}"),
})?;
self.decode(&bytes, ctx)
}
}