use std::borrow::Cow;
use alloy_primitives::{BlockNumber, TxHash};
use alloy_transport::TransportError;
#[derive(Debug, thiserror::Error)]
pub enum RpcError {
#[error("Failed to fetch logs for {operation}")]
GetLogsFailed {
operation: Cow<'static, str>,
#[source]
source: TransportError,
},
#[error("Transaction not found: {tx_hash}")]
TransactionNotFound {
tx_hash: TxHash,
},
#[error("Receipt not found for transaction: {tx_hash}")]
ReceiptNotFound {
tx_hash: TxHash,
},
#[error("Block not found: {block_number}")]
BlockNotFound {
block_number: BlockNumber,
},
#[deprecated(
since = "0.10.0",
note = "Prefer RpcError::RequestFailed for tx/receipt/log method failures that reached the provider transport stack"
)]
#[error("Chain connection failed during {operation}")]
ChainConnectionFailed {
operation: Cow<'static, str>,
#[source]
source: TransportError,
},
#[error("RPC request failed during {operation}")]
RequestFailed {
operation: Cow<'static, str>,
#[source]
source: TransportError,
},
#[error("RPC request timed out after {timeout_secs}s during {operation}")]
Timeout {
operation: Cow<'static, str>,
timeout_secs: u64,
},
#[error("Failed to get current block number")]
GetBlockNumberFailed {
#[source]
source: TransportError,
},
#[error("Failed to fetch block {block_number} details")]
GetBlockFailed {
block_number: BlockNumber,
#[source]
source: TransportError,
},
#[error("Subscription failed for {operation}: {details}")]
SubscriptionFailed {
operation: Cow<'static, str>,
details: String,
},
#[error("Invalid provider URL: {0}")]
ProviderUrlInvalid(String),
#[error("Failed to connect to provider: {0}")]
ProviderConnectionFailed(String),
}
impl RpcError {
pub fn get_logs_failed(
operation: impl Into<Cow<'static, str>>,
source: TransportError,
) -> Self {
RpcError::GetLogsFailed {
operation: operation.into(),
source,
}
}
#[deprecated(
since = "0.10.0",
note = "Prefer RpcError::request_failed for tx/receipt/log method failures that reached the provider transport stack"
)]
#[allow(deprecated)]
pub fn chain_connection_failed(
operation: impl Into<Cow<'static, str>>,
source: TransportError,
) -> Self {
RpcError::ChainConnectionFailed {
operation: operation.into(),
source,
}
}
pub fn request_failed(operation: impl Into<Cow<'static, str>>, source: TransportError) -> Self {
RpcError::RequestFailed {
operation: operation.into(),
source,
}
}
pub fn get_block_number_failed(source: TransportError) -> Self {
RpcError::GetBlockNumberFailed { source }
}
pub fn get_block_failed(block_number: BlockNumber, source: TransportError) -> Self {
RpcError::GetBlockFailed {
block_number,
source,
}
}
pub fn timeout(operation: impl Into<Cow<'static, str>>, timeout: std::time::Duration) -> Self {
RpcError::Timeout {
operation: operation.into(),
timeout_secs: timeout.as_secs(),
}
}
pub fn subscription_failed(
operation: impl Into<Cow<'static, str>>,
error: impl std::fmt::Display,
) -> Self {
RpcError::SubscriptionFailed {
operation: operation.into(),
details: error.to_string(),
}
}
}