Skip to main content

signet_tx_cache/
error.rs

1/// Result type for [`TxCache`] operations.
2///
3/// [`TxCache`]: crate::client::TxCache
4pub type Result<T> = std::result::Result<T, TxCacheError>;
5
6/// Errors returned by the [`TxCache`] client.
7///
8/// [`TxCache`]: crate::client::TxCache
9#[derive(thiserror::Error, Debug)]
10pub enum TxCacheError {
11    /// The requested transaction or bundle was not found in the cache.
12    #[error("Transaction not found in cache")]
13    NotFound,
14    /// The request was made during a slot that is not assigned to this builder.
15    #[error("Request occurred during a slot that is not assigned to this builder")]
16    NotOurSlot,
17    /// An error occurred while parsing the URL.
18    #[error(transparent)]
19    Url(#[from] url::ParseError),
20
21    /// An error occurred while contacting the TxCache API.
22    #[error("Error contacting TxCache API: {0}")]
23    Reqwest(reqwest::Error),
24
25    /// An error occurred while parsing SSE events.
26    #[cfg(feature = "sse")]
27    #[cfg_attr(docsrs, doc(cfg(feature = "sse")))]
28    #[error("SSE stream error: {0}")]
29    Sse(eventsource_stream::EventStreamError<reqwest::Error>),
30
31    /// Failed to deserialize an SSE event payload.
32    #[cfg(feature = "sse")]
33    #[cfg_attr(docsrs, doc(cfg(feature = "sse")))]
34    #[error("Failed to deserialize SSE event: {0}")]
35    Deserialization(serde_json::Error),
36}
37
38impl From<reqwest::Error> for TxCacheError {
39    fn from(err: reqwest::Error) -> Self {
40        match err.status() {
41            Some(reqwest::StatusCode::NOT_FOUND) => TxCacheError::NotFound,
42            Some(reqwest::StatusCode::FORBIDDEN) => TxCacheError::NotOurSlot,
43            _ => TxCacheError::Reqwest(err),
44        }
45    }
46}
47
48#[cfg(feature = "sse")]
49impl From<eventsource_stream::EventStreamError<reqwest::Error>> for TxCacheError {
50    fn from(err: eventsource_stream::EventStreamError<reqwest::Error>) -> Self {
51        Self::Sse(err)
52    }
53}
54
55#[cfg(feature = "sse")]
56impl From<serde_json::Error> for TxCacheError {
57    fn from(err: serde_json::Error) -> Self {
58        Self::Deserialization(err)
59    }
60}