Skip to main content

signet_cold/
error.rs

1//! Error types for cold storage operations.
2
3use std::time::Duration;
4
5/// Result type alias for cold storage operations.
6pub type ColdResult<T, E = ColdStorageError> = Result<T, E>;
7
8/// Error type for cold storage operations.
9#[derive(Debug, thiserror::Error)]
10pub enum ColdStorageError {
11    /// An error occurred in the storage backend.
12    #[error("Backend error: {0}")]
13    Backend(#[from] Box<dyn core::error::Error + Send + Sync + 'static>),
14
15    /// The requested resource was not found.
16    #[error("Not found: {0}")]
17    NotFound(String),
18
19    /// The query matched more logs than the caller-specified `max_logs` limit.
20    ///
21    /// The query is rejected entirely rather than returning a partial result.
22    /// Callers should narrow the filter (smaller block range, more specific
23    /// address/topic filters) or increase the limit.
24    #[error("too many logs: query exceeds {limit}")]
25    TooManyLogs {
26        /// The limit that was exceeded.
27        limit: usize,
28    },
29
30    /// A non-streaming read exceeded its configured deadline.
31    ///
32    /// Backends with native timeout support (iterator reads, pooled
33    /// clients) surface this as a first-class variant so callers can
34    /// match on it without downcasting [`Self::Backend`].
35    #[error("read deadline exceeded after {0:?}")]
36    DeadlineExceeded(Duration),
37
38    /// The streaming operation exceeded its deadline.
39    ///
40    /// The backend enforces a wall-clock limit on streaming operations
41    /// to prevent unbounded resource acquisition. Partial results may
42    /// have been delivered before this error.
43    #[error("stream deadline exceeded")]
44    StreamDeadlineExceeded,
45
46    /// A reorg was detected during a streaming operation.
47    ///
48    /// The anchor block hash changed between chunks, indicating that
49    /// the data being streamed may no longer be consistent. Partial
50    /// results may have been delivered before this error.
51    #[error("reorg detected during streaming")]
52    ReorgDetected,
53
54    /// The cold storage task has terminated.
55    ///
56    /// A concurrency semaphore was closed, indicating shutdown. Reads and
57    /// writes cannot be dispatched until the handle is recreated.
58    #[error("cold storage task terminated: semaphore closed")]
59    TaskTerminated,
60}
61
62impl ColdStorageError {
63    /// Create a new backend error from any error type.
64    pub fn backend<E>(error: E) -> Self
65    where
66        E: core::error::Error + Send + Sync + 'static,
67    {
68        Self::Backend(Box::new(error))
69    }
70
71    /// Short, stable label identifying the error variant. Used as a metric
72    /// label.
73    pub(crate) const fn kind(&self) -> &'static str {
74        match self {
75            Self::Backend(_) => "backend",
76            Self::NotFound(_) => "not_found",
77            Self::TooManyLogs { .. } => "too_many_logs",
78            Self::DeadlineExceeded(_) => "deadline",
79            Self::StreamDeadlineExceeded => "stream_deadline",
80            Self::ReorgDetected => "reorg",
81            Self::TaskTerminated => "task_terminated",
82        }
83    }
84}