breez_sdk_core/swap_out/
error.rs

1use sdk_common::prelude::ServiceConnectivityError;
2
3use crate::{
4    bitcoin::{hashes, script::PushBytesError, secp256k1},
5    error::SdkError,
6    node_api::NodeError,
7    persist::error::PersistError,
8};
9
10pub type ReverseSwapResult<T, E = ReverseSwapError> = Result<T, E>;
11
12#[derive(Debug, thiserror::Error)]
13pub enum ReverseSwapError {
14    #[error("{0}")]
15    Generic(String),
16
17    #[error("Claim tx feerate is too low")]
18    ClaimFeerateTooLow,
19
20    #[error("{0}")]
21    InvalidDestinationAddress(String),
22
23    #[error(transparent)]
24    Node(#[from] NodeError),
25
26    #[error("{0}")]
27    RouteNotFound(String),
28
29    #[error("{0}")]
30    ServiceConnectivity(String),
31
32    #[error("{0}")]
33    UnexpectedInvoiceAmount(String),
34
35    #[error("Unexpected lockup address")]
36    UnexpectedLockupAddress,
37
38    #[error("{0}")]
39    UnexpectedPaymentHash(String),
40
41    #[error("Unexpected redeem script")]
42    UnexpectedRedeemScript,
43}
44
45impl ReverseSwapError {
46    pub(crate) fn generic(err: &str) -> Self {
47        Self::Generic(err.to_string())
48    }
49
50    pub(crate) fn unexpected_invoice_amount(err: &str) -> Self {
51        Self::UnexpectedInvoiceAmount(err.to_string())
52    }
53
54    pub(crate) fn unexpected_payment_hash(err: &str) -> Self {
55        Self::UnexpectedPaymentHash(err.to_string())
56    }
57}
58
59impl From<anyhow::Error> for ReverseSwapError {
60    fn from(err: anyhow::Error) -> Self {
61        Self::Generic(err.to_string())
62    }
63}
64
65impl From<hashes::hex::Error> for ReverseSwapError {
66    fn from(err: hashes::hex::Error) -> Self {
67        Self::Generic(err.to_string())
68    }
69}
70
71impl From<bitcoin::absolute::Error> for ReverseSwapError {
72    fn from(err: bitcoin::absolute::Error) -> Self {
73        Self::Generic(err.to_string())
74    }
75}
76
77impl From<PushBytesError> for ReverseSwapError {
78    fn from(err: PushBytesError) -> Self {
79        Self::Generic(err.to_string())
80    }
81}
82
83impl From<hex::FromHexError> for ReverseSwapError {
84    fn from(err: hex::FromHexError) -> Self {
85        Self::Generic(err.to_string())
86    }
87}
88
89impl From<crate::lightning_invoice::ParseOrSemanticError> for ReverseSwapError {
90    fn from(err: crate::lightning_invoice::ParseOrSemanticError) -> Self {
91        Self::Generic(err.to_string())
92    }
93}
94
95impl From<PersistError> for ReverseSwapError {
96    fn from(err: PersistError) -> Self {
97        Self::Generic(err.to_string())
98    }
99}
100
101impl From<reqwest::Error> for ReverseSwapError {
102    fn from(err: reqwest::Error) -> Self {
103        Self::Generic(err.to_string())
104    }
105}
106
107impl From<ServiceConnectivityError> for ReverseSwapError {
108    fn from(value: ServiceConnectivityError) -> Self {
109        Self::ServiceConnectivity(value.err)
110    }
111}
112
113impl From<SdkError> for ReverseSwapError {
114    fn from(value: SdkError) -> Self {
115        match value {
116            SdkError::Generic { err } => Self::Generic(err),
117            SdkError::ServiceConnectivity { err } => Self::ServiceConnectivity(err),
118        }
119    }
120}
121
122impl From<secp256k1::Error> for ReverseSwapError {
123    fn from(err: secp256k1::Error) -> Self {
124        Self::Generic(err.to_string())
125    }
126}
127
128impl From<serde_json::Error> for ReverseSwapError {
129    fn from(err: serde_json::Error) -> Self {
130        Self::ServiceConnectivity(err.to_string())
131    }
132}
133
134impl From<tonic::Status> for ReverseSwapError {
135    fn from(err: tonic::Status) -> Self {
136        Self::Generic(sdk_common::tonic_wrap::Status(err).to_string())
137    }
138}