Skip to main content

uts_sdk/
error.rs

1use alloy_primitives::{ChainId, private::serde::de::StdError};
2
3pub(crate) type BoxError = Box<dyn StdError + Send + Sync>;
4
5/// Error type for the SDK, encompassing various error scenarios that can occur during operations.
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8    /// Error during an I/O operation, such as reading or writing files.
9    #[error("I/O error: {0}")]
10    Io(#[from] std::io::Error),
11    /// Error during an HTTP request, such as a network failure or non-success status code.
12    #[error("HTTP error: {0}")]
13    Http(BoxError),
14    /// Error parsing a URL, such as a calendar endpoint.
15    #[error("URL parse error: {0}")]
16    Url(#[from] url::ParseError),
17    /// Error during time-related operations, such as calculating timestamps or durations.
18    #[error("Time error: {0}")]
19    Jiff(#[from] jiff::Error),
20
21    /// Error happened during decoding of a proof.
22    #[error("uts decoding error: {0}")]
23    Decode(#[from] uts_core::error::DecodeError),
24    /// Error happened during encoding of a proof.
25    #[error("uts encoding error: {0}")]
26    Encode(#[from] uts_core::error::EncodeError),
27    /// Error indicating that finalization of a timestamp failed due to conflicting inputs.
28    #[error(transparent)]
29    Finalization(#[from] uts_core::codec::v1::FinalizationError),
30
31    /// Error indicating that the input provided was empty when it was expected to contain data.
32    #[error("Input cannot be empty")]
33    EmptyInput,
34
35    /// Error indicating that a quorum of responses was not reached from the calendars.
36    #[error("Quorum of {required} not reached, only {received} responses received")]
37    QuorumNotReached {
38        /// Number of responses required to reach quorum
39        required: usize,
40        /// Number of responses actually received
41        received: usize,
42    },
43
44    /// Error indicating that a digest mismatch occurred, with expected and actual digest values.
45    #[error("Digest mismatch: expected {expected:?}, got {actual:?}")]
46    DigestMismatch {
47        /// Expected digest value
48        expected: Box<[u8]>,
49        /// Actual digest value
50        actual: Box<[u8]>,
51    },
52
53    /// Error indicating that an unsupported feature was encountered, with a message describing the unsupported feature.
54    #[error("Unsupported feature: {0}")]
55    Unsupported(&'static str),
56    /// Error indicating that an unsupported chain ID was encountered.
57    #[error("Unsupported chain ID: {0}")]
58    UnsupportedChain(ChainId),
59}
60
61impl From<reqwest::Error> for Error {
62    fn from(value: reqwest::Error) -> Self {
63        Self::Http(Box::new(value))
64    }
65}
66
67impl From<http::Error> for Error {
68    fn from(value: http::Error) -> Self {
69        Self::Http(Box::new(value))
70    }
71}