tempo_node/rpc/dex/
error.rs

1use alloy_eips::BlockId;
2use alloy_primitives::B256;
3use jsonrpsee::types::ErrorObject;
4use reth_rpc_eth_types::{EthApiError, error::ToRpcError};
5use tempo_precompiles::error::TempoPrecompileError;
6
7/// DEX API specific errors that extend [`EthApiError`].
8#[derive(Debug, thiserror::Error)]
9pub enum DexApiError {
10    /// Precompile storage errors
11    #[error(transparent)]
12    Precompile(#[from] TempoPrecompileError),
13
14    /// Header not found for block
15    #[error("header not found for block {0:?}")]
16    HeaderNotFound(BlockId),
17
18    /// Provider error when getting header
19    /// Boxed because Provider::Error is an associated type
20    #[error("internal node error: failed to get header: {0}")]
21    Provider(#[source] Box<dyn std::error::Error + Send + Sync>),
22
23    /// Failed to create EVM context
24    /// Boxed because ConfigureEvm::Error is an associated type
25    #[error("internal node error: failed to create EVM")]
26    CreateEvm(#[source] Box<dyn std::error::Error + Send + Sync>),
27
28    /// Invalid hex string in order cursor
29    #[error("invalid order cursor: expected hex string, got {0}")]
30    InvalidOrderCursor(String),
31
32    /// Failed to parse order cursor as u128
33    #[error("invalid order cursor: failed to parse hex value")]
34    ParseOrderCursor(#[from] std::num::ParseIntError),
35
36    /// Invalid orderbook cursor format
37    #[error("invalid orderbook cursor: failed to parse as B256")]
38    InvalidOrderbookCursor(String),
39
40    /// Orderbook cursor not found in available books
41    #[error("orderbook cursor {0} not found in available books")]
42    OrderbookCursorNotFound(B256),
43}
44
45impl DexApiError {
46    /// Returns the rpc error for this error
47    const fn error_code(&self) -> i32 {
48        match self {
49            Self::InvalidOrderbookCursor(_)
50            | Self::InvalidOrderCursor(_)
51            | Self::ParseOrderCursor(_) => jsonrpsee::types::error::INVALID_PARAMS_CODE,
52            _ => jsonrpsee::types::error::INTERNAL_ERROR_CODE,
53        }
54    }
55}
56
57impl From<DexApiError> for EthApiError {
58    fn from(err: DexApiError) -> Self {
59        match err {
60            DexApiError::HeaderNotFound(block_id) => Self::HeaderNotFound(block_id),
61            // All other errors use the Other variant with our error type
62            other => Self::other(other),
63        }
64    }
65}
66
67impl ToRpcError for DexApiError {
68    fn to_rpc_error(&self) -> ErrorObject<'static> {
69        ErrorObject::owned(self.error_code(), self.to_string(), None::<()>)
70    }
71}
72
73impl From<DexApiError> for ErrorObject<'static> {
74    fn from(value: DexApiError) -> Self {
75        value.to_rpc_error()
76    }
77}