essential_relayer/
error.rs

1use essential_types::{ContentAddress, Word};
2use thiserror::Error;
3
4/// The result type for the relayer.
5pub type Result<T> = std::result::Result<T, Error>;
6
7/// The result type for internal errors.
8pub(crate) type InternalResult<T> = std::result::Result<T, InternalError>;
9
10/// Critical or recoverable errors that can occur in the relayer.
11#[derive(Debug, Error)]
12pub(crate) enum InternalError {
13    /// A critical error occurred.
14    #[error("a critical error occurred: {0}")]
15    Critical(#[from] Error),
16    /// A recoverable error occurred.
17    #[error("a recoverable error occurred: {0}")]
18    Recoverable(#[from] RecoverableError),
19}
20
21/// Alias for a critical error.
22pub(crate) type CriticalError = Error;
23
24/// An error occurred in the relayer that is not recoverable.
25/// These causes the relayer to exit a spawned task.
26#[derive(Debug, Error)]
27pub enum Error {
28    /// Failed to parse a server url.
29    #[error("an error occurred when parsing the server url")]
30    UrlParse,
31    /// An overflow occurred.
32    #[error("an overflow occurred when converting a number")]
33    Overflow,
34    /// A data sync error occurred.
35    #[error("a data sync error occurred: {0}")]
36    DataSyncFailed(#[from] DataSyncError),
37    /// An error occurred while building the http client.
38    #[error("an error occurred while building the http client: {0}")]
39    HttpClientBuild(reqwest::Error),
40    /// Failed to acquire then use a DB connection from the pool.
41    #[error("failed to acquire or use a DB connection: {0}")]
42    DbPoolRusqlite(#[from] essential_node_db::pool::AcquireThenRusqliteError),
43}
44
45/// An error that can be recovered from.
46/// The stream will restart after logging a recoverable error.
47#[derive(Debug, Error)]
48pub(crate) enum RecoverableError {
49    /// Stream from server failed.
50    #[error("an error occurred in the stream from the server: {0}")]
51    Stream(#[from] std::io::Error),
52    /// Failed to make a request to the server.
53    #[error("an error occurred in a request to the server: {0}")]
54    BadServerResponse(reqwest::StatusCode),
55    /// Http client error.
56    #[error("an error occurred in the http client: {0}")]
57    HttpClient(#[from] reqwest::Error),
58    /// A new block was not sequentially after the last block.
59    #[error("a new block was not sequentially after the last block. Got: {0}, expected: {1}")]
60    NonSequentialBlock(Word, Word),
61    /// The stream returned an error.
62    #[error("the stream returned an error: {0}")]
63    StreamError(String),
64    /// A DB error occurred.
65    #[error("a DB error occurred: {0}")]
66    Rusqlite(rusqlite::Error),
67}
68
69#[derive(Debug, Error)]
70/// An error occurred while syncing data.
71pub enum DataSyncError {
72    /// A fork was detected while syncing blocks.
73    #[error(
74        "While syncing blocks a fork was detected at block number {0}. Got: {1}, expected: {}", display_address(.2)
75    )]
76    Fork(Word, ContentAddress, Option<ContentAddress>),
77}
78
79fn display_address<T>(addr: &Option<T>) -> String
80where
81    T: core::fmt::Display,
82{
83    match addr {
84        Some(addr) => format!("{}", addr),
85        None => "None".to_string(),
86    }
87}
88
89impl From<std::io::Error> for InternalError {
90    fn from(e: std::io::Error) -> Self {
91        InternalError::Recoverable(RecoverableError::Stream(e))
92    }
93}