1use crate::{
2 Fee,
3 persist::{self},
4};
5use bitcoin::consensus::encode::FromHexError;
6use serde::{Deserialize, Serialize};
7use spark_wallet::SparkWalletError;
8use std::{convert::Infallible, num::TryFromIntError};
9use thiserror::Error;
10
11#[derive(Debug, Error, Clone)]
13#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
14pub enum SdkError {
15 #[error("SparkSdkError: {0}")]
16 SparkError(String),
17
18 #[error("Invalid UUID: {0}")]
19 InvalidUuid(String),
20
21 #[error("Invalid input: {0}")]
23 InvalidInput(String),
24
25 #[error("Network error: {0}")]
27 NetworkError(String),
28
29 #[error("Storage error: {0}")]
31 StorageError(String),
32
33 #[error("Chain service error: {0}")]
34 ChainServiceError(String),
35
36 #[error(
37 "Deposit claim fee exceeds for utxo: {tx}:{vout} with max fee: {max_fee} and actual fee sat: {actual_fee}"
38 )]
39 DepositClaimFeeExceeded {
40 tx: String,
41 vout: u32,
42 max_fee: Fee,
43 actual_fee: u64,
44 },
45
46 #[error("Missing utxo: {tx}:{vout}")]
47 MissingUtxo { tx: String, vout: u32 },
48
49 #[error("Lnurl error: {0}")]
50 LnurlError(String),
51
52 #[error("Error: {0}")]
53 Generic(String),
54}
55
56impl From<crate::chain::ChainServiceError> for SdkError {
57 fn from(e: crate::chain::ChainServiceError) -> Self {
58 SdkError::ChainServiceError(e.to_string())
59 }
60}
61
62impl From<breez_sdk_common::lnurl::error::LnurlError> for SdkError {
63 fn from(e: breez_sdk_common::lnurl::error::LnurlError) -> Self {
64 SdkError::LnurlError(e.to_string())
65 }
66}
67
68impl From<breez_sdk_common::input::ParseError> for SdkError {
69 fn from(e: breez_sdk_common::input::ParseError) -> Self {
70 SdkError::InvalidInput(e.to_string())
71 }
72}
73
74impl From<bitcoin::address::ParseError> for SdkError {
75 fn from(e: bitcoin::address::ParseError) -> Self {
76 SdkError::InvalidInput(e.to_string())
77 }
78}
79
80impl From<persist::StorageError> for SdkError {
81 fn from(e: persist::StorageError) -> Self {
82 SdkError::StorageError(e.to_string())
83 }
84}
85
86impl From<Infallible> for SdkError {
87 fn from(value: Infallible) -> Self {
88 SdkError::Generic(value.to_string())
89 }
90}
91
92impl From<String> for SdkError {
93 fn from(s: String) -> Self {
94 Self::Generic(s)
95 }
96}
97
98impl From<&str> for SdkError {
99 fn from(s: &str) -> Self {
100 Self::Generic(s.to_string())
101 }
102}
103
104impl From<TryFromIntError> for SdkError {
105 fn from(e: TryFromIntError) -> Self {
106 SdkError::Generic(e.to_string())
107 }
108}
109
110impl From<serde_json::Error> for SdkError {
111 fn from(e: serde_json::Error) -> Self {
112 SdkError::Generic(e.to_string())
113 }
114}
115
116impl From<SparkWalletError> for SdkError {
117 fn from(e: SparkWalletError) -> Self {
118 SdkError::SparkError(e.to_string())
119 }
120}
121
122impl From<FromHexError> for SdkError {
123 fn from(e: FromHexError) -> Self {
124 SdkError::Generic(e.to_string())
125 }
126}
127
128impl From<uuid::Error> for SdkError {
129 fn from(e: uuid::Error) -> Self {
130 SdkError::InvalidUuid(e.to_string())
131 }
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize, Error, PartialEq)]
135#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
136pub enum DepositClaimError {
137 #[error(
138 "Deposit claim fee exceeds for utxo: {tx}:{vout} with max fee: {max_fee} and actual fee sat: {actual_fee}"
139 )]
140 DepositClaimFeeExceeded {
141 tx: String,
142 vout: u32,
143 max_fee: Fee,
144 actual_fee: u64,
145 },
146
147 #[error("Missing utxo: {tx}:{vout}")]
148 MissingUtxo { tx: String, vout: u32 },
149
150 #[error("Generic error: {message}")]
151 Generic { message: String },
152}
153
154impl From<SdkError> for DepositClaimError {
155 fn from(value: SdkError) -> Self {
156 match value {
157 SdkError::DepositClaimFeeExceeded {
158 tx,
159 vout,
160 max_fee,
161 actual_fee,
162 } => DepositClaimError::DepositClaimFeeExceeded {
163 tx,
164 vout,
165 max_fee,
166 actual_fee,
167 },
168 SdkError::MissingUtxo { tx, vout } => DepositClaimError::MissingUtxo { tx, vout },
169 SdkError::Generic(e) => DepositClaimError::Generic { message: e },
170 _ => DepositClaimError::Generic {
171 message: value.to_string(),
172 },
173 }
174 }
175}