breez_sdk_liquid/payjoin/
error.rs

1use lwk_wollet::{bitcoin, elements};
2use sdk_common::prelude::ServiceConnectivityError;
3
4use crate::error::PaymentError;
5
6pub type PayjoinResult<T, E = PayjoinError> = Result<T, E>;
7
8#[derive(Debug, thiserror::Error)]
9pub enum PayjoinError {
10    #[error("{0}")]
11    Generic(String),
12
13    #[error("Cannot pay: not enough funds")]
14    InsufficientFunds,
15
16    #[error("{0}")]
17    ServiceConnectivity(String),
18}
19
20impl PayjoinError {
21    pub(crate) fn generic<S: AsRef<str>>(err: S) -> Self {
22        Self::Generic(err.as_ref().to_string())
23    }
24
25    pub(crate) fn service_connectivity<S: AsRef<str>>(err: S) -> Self {
26        Self::ServiceConnectivity(err.as_ref().to_string())
27    }
28}
29
30impl From<anyhow::Error> for PayjoinError {
31    fn from(err: anyhow::Error) -> Self {
32        Self::Generic(err.to_string())
33    }
34}
35
36impl From<bitcoin::base64::DecodeError> for PayjoinError {
37    fn from(err: bitcoin::base64::DecodeError) -> Self {
38        Self::Generic(err.to_string())
39    }
40}
41
42impl From<elements::encode::Error> for PayjoinError {
43    fn from(err: elements::encode::Error) -> Self {
44        Self::Generic(err.to_string())
45    }
46}
47
48impl From<elements::hashes::hex::HexToArrayError> for PayjoinError {
49    fn from(err: elements::hashes::hex::HexToArrayError) -> Self {
50        Self::Generic(err.to_string())
51    }
52}
53
54impl From<PaymentError> for PayjoinError {
55    fn from(value: PaymentError) -> Self {
56        match value {
57            PaymentError::InsufficientFunds => Self::InsufficientFunds,
58            _ => Self::Generic(value.to_string()),
59        }
60    }
61}
62
63impl From<serde_json::error::Error> for PayjoinError {
64    fn from(value: serde_json::error::Error) -> Self {
65        Self::Generic(value.to_string())
66    }
67}
68
69impl From<ServiceConnectivityError> for PayjoinError {
70    fn from(value: ServiceConnectivityError) -> Self {
71        Self::ServiceConnectivity(value.err)
72    }
73}