|
| 1 | +use std::{ops::RangeInclusive, sync::Arc}; |
| 2 | + |
| 3 | +use alloy::{ |
| 4 | + eips::BlockNumberOrTag, |
| 5 | + primitives::BlockNumber, |
| 6 | + transports::{RpcError, TransportErrorKind, http::reqwest}, |
| 7 | +}; |
| 8 | +use thiserror::Error; |
| 9 | + |
| 10 | +use crate::block_range_scanner::Message; |
| 11 | + |
| 12 | +#[derive(Error, Debug, Clone)] |
| 13 | +pub enum ScannerError { |
| 14 | + #[error("HTTP request failed: {0}")] |
| 15 | + HttpError(Arc<reqwest::Error>), |
| 16 | + |
| 17 | + // #[error("WebSocket error: {0}")] |
| 18 | + // WebSocketError(#[from] tokio_tungstenite::tungstenite::Error), |
| 19 | + #[error("Serialization error: {0}")] |
| 20 | + SerializationError(Arc<serde_json::Error>), |
| 21 | + |
| 22 | + #[error("RPC error: {0}")] |
| 23 | + RpcError(Arc<RpcError<TransportErrorKind>>), |
| 24 | + |
| 25 | + #[error("Channel send error")] |
| 26 | + ChannelError, |
| 27 | + |
| 28 | + #[error("Service is shutting down")] |
| 29 | + ServiceShutdown, |
| 30 | + |
| 31 | + #[error("Only one subscriber allowed at a time")] |
| 32 | + MultipleSubscribers, |
| 33 | + |
| 34 | + #[error("No subscriber set for streaming")] |
| 35 | + NoSubscriber, |
| 36 | + |
| 37 | + #[error("Historical sync failed: {0}")] |
| 38 | + HistoricalSyncError(String), |
| 39 | + |
| 40 | + #[error("WebSocket connection failed after {0} attempts")] |
| 41 | + WebSocketConnectionFailed(usize), |
| 42 | + |
| 43 | + #[error("Block not found, block number: {0}")] |
| 44 | + BlockNotFound(BlockNumberOrTag), |
| 45 | +} |
| 46 | + |
| 47 | +impl From<Result<RangeInclusive<BlockNumber>, ScannerError>> for Message { |
| 48 | + fn from(logs: Result<RangeInclusive<BlockNumber>, ScannerError>) -> Self { |
| 49 | + match logs { |
| 50 | + Ok(logs) => Message::Data(logs), |
| 51 | + Err(e) => Message::Error(e), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl From<reqwest::Error> for ScannerError { |
| 57 | + fn from(error: reqwest::Error) -> Self { |
| 58 | + ScannerError::HttpError(Arc::new(error)) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +impl From<serde_json::Error> for ScannerError { |
| 63 | + fn from(error: serde_json::Error) -> Self { |
| 64 | + ScannerError::SerializationError(Arc::new(error)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +impl From<RpcError<TransportErrorKind>> for ScannerError { |
| 69 | + fn from(error: RpcError<TransportErrorKind>) -> Self { |
| 70 | + ScannerError::RpcError(Arc::new(error)) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +impl From<ScannerError> for Message { |
| 75 | + fn from(error: ScannerError) -> Self { |
| 76 | + Message::Error(error) |
| 77 | + } |
| 78 | +} |
0 commit comments