Skip to main content

hopr_lib/
errors.rs

1use hopr_api::node::HoprState;
2pub use hopr_network_types::errors::NetworkTypeError;
3pub use hopr_transport::errors::{HoprTransportError, ProbeError, ProtocolError};
4use thiserror::Error;
5
6/// Enumeration of errors thrown from this library.
7///
8/// Consumers should prefer matching on specific variants over catching
9/// [`GeneralError`](Self::GeneralError). Transport and network sub-variant types
10/// ([`HoprTransportError`], [`ProbeError`], [`ProtocolError`], [`NetworkTypeError`])
11/// are re-exported from this module for convenient pattern matching.
12#[derive(Error, Debug)]
13pub enum HoprLibError {
14    /// A general-purpose error with a descriptive message.
15    ///
16    /// Prefer matching on specific variants when possible.
17    #[error("HOPR lib error: '{0}'")]
18    GeneralError(String),
19
20    /// The [`Hopr`](crate::Hopr) object could not be built due to a missing component.
21    #[error("hopr object could not be built: {0}")]
22    BuilderError(&'static str),
23
24    /// The node configuration failed validation.
25    #[error("configuration validation failed: {0}")]
26    ConfigurationError(#[from] validator::ValidationErrors),
27
28    /// An error originating from the blockchain/chain connector layer.
29    ///
30    /// The inner error is type-erased because the chain connector is injected
31    /// via trait objects. Use the `Display` output for diagnostics.
32    #[error("chain error: {0}")]
33    ChainError(#[source] anyhow::Error),
34
35    /// The node is not in the required operational state.
36    ///
37    /// Contains the current [`HoprState`] and a message describing what was expected.
38    /// REST API consumers typically map this to HTTP 412 Precondition Failed.
39    #[error("node not ready ({0}): {1}")]
40    NotReady(HoprState, String),
41
42    /// An operation timed out.
43    #[error("{context} timed out")]
44    Timeout {
45        /// Description of what timed out and how long it waited.
46        context: String,
47    },
48
49    /// The node's wallet has insufficient native token balance.
50    #[error("insufficient funds: {0}")]
51    InsufficientFunds(String),
52
53    /// An error from the transport layer (messaging, sessions, probing).
54    ///
55    /// Sub-variant types [`ProtocolError`] and [`ProbeError`] are re-exported
56    /// from this module for convenient pattern matching.
57    #[error(transparent)]
58    TransportError(#[from] HoprTransportError),
59
60    /// A network-layer type error (address parsing, sealed targets, IO).
61    ///
62    /// The [`NetworkTypeError`] type is re-exported from this module.
63    #[error(transparent)]
64    NetworkTypeError(#[from] NetworkTypeError),
65
66    /// An unclassified internal error.
67    ///
68    /// Used for subsystems where a typed error variant does not exist.
69    /// Use the `Display` output for diagnostics.
70    #[error("unspecified error: {0}")]
71    Other(#[source] anyhow::Error),
72}
73
74pub type Result<T> = std::result::Result<T, HoprLibError>;
75
76impl HoprLibError {
77    pub fn chain<E: Into<anyhow::Error>>(e: E) -> Self {
78        Self::ChainError(e.into())
79    }
80
81    pub fn other(e: impl Into<anyhow::Error>) -> Self {
82        Self::Other(e.into())
83    }
84}