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("type conversion error: {0}")]
32    TypeConversion(String),
33
34    #[error("timeout while waiting for the graph to be synced")]
35    ConnectionTimeout,
36
37    #[error("backend error: {0}")]
38    BackendError(#[source] anyhow::Error),
39
40    #[error(transparent)]
41    CacheError(#[from] std::sync::Arc<Self>),
42
43    #[error(transparent)]
44    ClientError(#[from] blokli_client::errors::BlokliClientError),
45
46    #[error(transparent)]
47    GeneralError(#[from] hopr_api::types::primitive::errors::GeneralError),
48
49    #[error(transparent)]
50    ChainTypesError(#[from] hopr_api::types::chain::errors::ChainTypesError),
51
52    #[error(transparent)]
53    CoreTypesError(#[from] hopr_api::types::internal::errors::CoreTypesError),
54
55    #[error(transparent)]
56    IoError(#[from] std::io::Error),
57
58    #[error("undefined error: {0}")]
59    OtherError(#[source] anyhow::Error),
60}
61
62impl ConnectorError {
63    /// Indicates whether this error was caused by the transaction actually being rejected
64    /// by the target blockchain and returns the errors.
65    pub fn as_transaction_rejection_error(&self) -> Option<&TrackingErrorKind> {
66        match self {
67            ConnectorError::ClientError(client_error) => match client_error.kind() {
68                ErrorKind::TrackingError(e @ TrackingErrorKind::Reverted)
69                | ErrorKind::TrackingError(e @ TrackingErrorKind::ValidationFailed) => Some(e),
70                _ => None,
71            },
72            _ => None,
73        }
74    }
75
76    pub fn other(e: impl Into<anyhow::Error>) -> Self {
77        Self::OtherError(e.into())
78    }
79
80    pub fn backend(e: impl Into<anyhow::Error>) -> Self {
81        Self::BackendError(e.into())
82    }
83
84    pub fn io(e: impl Into<std::io::Error>) -> Self {
85        Self::IoError(e.into())
86    }
87}
88
89pub type Result<T> = std::result::Result<T, ConnectorError>;