Skip to main content

hopr_chain_connector/
errors.rs

1use blokli_client::errors::{ErrorKind, TrackingErrorKind};
2use hopr_api::types::{internal::prelude::ChannelId, primitive::prelude::Address};
3use thiserror::Error;
4
5#[derive(Debug, Error)]
6pub enum ConnectorError {
7    #[error("invalid arguments: {0}")]
8    InvalidArguments(&'static str),
9
10    #[error("invalid state: {0}")]
11    InvalidState(&'static str),
12
13    #[error("blokli server is not healthy")]
14    ServerNotHealthy,
15
16    #[error("account {0} does not exist")]
17    AccountDoesNotExist(String),
18
19    #[error("safe {0} does not exist")]
20    SafeDoesNotExist(Address),
21
22    #[error("channel {0} does not exist")]
23    ChannelDoesNotExist(ChannelId),
24
25    #[error("ticket is invalid or does not match the channel")]
26    InvalidTicket,
27
28    #[error("channel {0} is closed")]
29    ChannelClosed(ChannelId),
30
31    #[error("inner safe transaction failed: {0}")]
32    InnerTxFailed(String),
33
34    #[error("type conversion error: {0}")]
35    TypeConversion(String),
36
37    #[error("timeout while waiting for the graph to be synced")]
38    ConnectionTimeout,
39
40    #[error("backend error: {0}")]
41    BackendError(#[source] anyhow::Error),
42
43    #[error(transparent)]
44    CacheError(#[from] std::sync::Arc<Self>),
45
46    #[error(transparent)]
47    ClientError(#[from] blokli_client::errors::BlokliClientError),
48
49    #[error(transparent)]
50    GeneralError(#[from] hopr_api::types::primitive::errors::GeneralError),
51
52    #[error(transparent)]
53    ChainTypesError(#[from] hopr_api::types::chain::errors::ChainTypesError),
54
55    #[error(transparent)]
56    CoreTypesError(#[from] hopr_api::types::internal::errors::CoreTypesError),
57
58    #[error(transparent)]
59    IoError(#[from] std::io::Error),
60
61    #[error("undefined error: {0}")]
62    OtherError(#[source] anyhow::Error),
63}
64
65impl ConnectorError {
66    /// Indicates whether this error was caused by the transaction actually being rejected
67    /// by the target blockchain and returns the errors.
68    pub fn as_transaction_rejection_error(&self) -> Option<&TrackingErrorKind> {
69        match self {
70            ConnectorError::ClientError(client_error) => match client_error.kind() {
71                ErrorKind::TrackingError(e @ TrackingErrorKind::Reverted)
72                | ErrorKind::TrackingError(e @ TrackingErrorKind::ValidationFailed) => Some(e),
73                _ => None,
74            },
75            _ => None,
76        }
77    }
78
79    pub fn other(e: impl Into<anyhow::Error>) -> Self {
80        Self::OtherError(e.into())
81    }
82
83    pub fn backend(e: impl Into<anyhow::Error>) -> Self {
84        Self::BackendError(e.into())
85    }
86
87    pub fn io(e: impl Into<std::io::Error>) -> Self {
88        Self::IoError(e.into())
89    }
90}
91
92pub type Result<T> = std::result::Result<T, ConnectorError>;