reth_optimism_txpool/supervisor/
errors.rs

1use alloy_json_rpc::RpcError;
2use core::error;
3use op_alloy_rpc_types::SuperchainDAError;
4
5/// Failures occurring during validation of inbox entries.
6#[derive(thiserror::Error, Debug)]
7pub enum InteropTxValidatorError {
8    /// Inbox entry validation against the Supervisor took longer than allowed.
9    #[error("inbox entry validation timed out, timeout: {0} secs")]
10    Timeout(u64),
11
12    /// Message does not satisfy validation requirements
13    #[error(transparent)]
14    InvalidEntry(#[from] SuperchainDAError),
15
16    /// Catch-all variant.
17    #[error("supervisor server error: {0}")]
18    Other(Box<dyn error::Error + Send + Sync>),
19}
20
21impl InteropTxValidatorError {
22    /// Returns a new instance of [`Other`](Self::Other) error variant.
23    pub fn other<E>(err: E) -> Self
24    where
25        E: error::Error + Send + Sync + 'static,
26    {
27        Self::Other(Box::new(err))
28    }
29
30    /// This function will parse the error code to determine if it matches
31    /// one of the known Supervisor errors, and return the corresponding
32    /// error variant. Otherwise, it returns a generic [`Other`](Self::Other) error.
33    pub fn from_json_rpc<E>(err: RpcError<E>) -> Self
34    where
35        E: error::Error + Send + Sync + 'static,
36    {
37        // Try to extract error details from the RPC error
38        if let Some(error_payload) = err.as_error_resp() {
39            let code = error_payload.code as i32;
40
41            // Try to convert the error code to an SuperchainDAError variant
42            if let Ok(invalid_entry) = SuperchainDAError::try_from(code) {
43                return Self::InvalidEntry(invalid_entry);
44            }
45        }
46
47        // Default to generic error
48        Self::Other(Box::new(err))
49    }
50}