lwk/
error.rs

1use std::sync::{MutexGuard, PoisonError};
2
3use elements::pset::ParseError;
4
5/// Possible errors emitted
6#[derive(uniffi::Error, thiserror::Error, Debug)]
7#[allow(missing_docs)]
8pub enum LwkError {
9    #[error("{msg}")]
10    Generic { msg: String },
11
12    #[error("Poison error: {msg}")]
13    PoisonError { msg: String },
14
15    #[error("Invoice contain a magic routing hint, there is no need to pay via Boltz, pay directly to: {uri}")]
16    MagicRoutingHint {
17        address: String,
18        amount: u64,
19        uri: String,
20    },
21
22    #[error("Swap {swap_id} has expired with status {status}")]
23    SwapExpired { swap_id: String, status: String },
24
25    #[error("There are no message to receive on the boltz web socket, continuing polling")]
26    NoBoltzUpdate,
27
28    #[error("Calling a function on an object that has already been consumed, like for example calling complete() on object that already is completed")]
29    ObjectConsumed,
30}
31
32impl From<lwk_wollet::Error> for LwkError {
33    fn from(value: lwk_wollet::Error) -> Self {
34        LwkError::Generic {
35            msg: format!("{value:?}"),
36        }
37    }
38}
39
40impl From<ParseError> for LwkError {
41    fn from(value: ParseError) -> Self {
42        LwkError::Generic {
43            msg: format!("{value:?}"),
44        }
45    }
46}
47
48impl From<elements::pset::Error> for LwkError {
49    fn from(value: elements::pset::Error) -> Self {
50        LwkError::Generic {
51            msg: format!("{value:?}"),
52        }
53    }
54}
55
56impl From<elements::encode::Error> for LwkError {
57    fn from(value: elements::encode::Error) -> Self {
58        LwkError::Generic {
59            msg: format!("{value:?}"),
60        }
61    }
62}
63
64impl From<elements::bitcoin::transaction::ParseOutPointError> for LwkError {
65    fn from(value: elements::bitcoin::transaction::ParseOutPointError) -> Self {
66        LwkError::Generic {
67            msg: format!("{value:?}"),
68        }
69    }
70}
71
72impl From<elements::bitcoin::key::FromWifError> for LwkError {
73    fn from(value: elements::bitcoin::key::FromWifError) -> Self {
74        LwkError::Generic {
75            msg: format!("{value:?}"),
76        }
77    }
78}
79
80impl From<elements::hashes::hex::HexToBytesError> for LwkError {
81    fn from(value: elements::hashes::hex::HexToBytesError) -> Self {
82        LwkError::Generic {
83            msg: format!("{value:?}"),
84        }
85    }
86}
87
88impl From<elements::hashes::hex::HexToArrayError> for LwkError {
89    fn from(value: elements::hashes::hex::HexToArrayError) -> Self {
90        LwkError::Generic {
91            msg: format!("{value:?}"),
92        }
93    }
94}
95
96impl From<elements::AddressError> for LwkError {
97    fn from(value: elements::AddressError) -> Self {
98        LwkError::Generic {
99            msg: format!("{value:?}"),
100        }
101    }
102}
103
104impl From<lwk_signer::bip39::Error> for LwkError {
105    fn from(value: lwk_signer::bip39::Error) -> Self {
106        LwkError::Generic {
107            msg: format!("{value:?}"),
108        }
109    }
110}
111
112impl From<lwk_signer::NewError> for LwkError {
113    fn from(value: lwk_signer::NewError) -> Self {
114        LwkError::Generic {
115            msg: format!("{value:?}"),
116        }
117    }
118}
119
120impl From<lwk_signer::SignError> for LwkError {
121    fn from(value: lwk_signer::SignError) -> Self {
122        LwkError::Generic {
123            msg: format!("{value:?}"),
124        }
125    }
126}
127
128impl From<serde_json::Error> for LwkError {
129    fn from(value: serde_json::Error) -> Self {
130        LwkError::Generic {
131            msg: format!("{value:?}"),
132        }
133    }
134}
135
136impl From<lwk_common::QrError> for LwkError {
137    fn from(value: lwk_common::QrError) -> Self {
138        LwkError::Generic {
139            msg: format!("{value:?}"),
140        }
141    }
142}
143
144impl From<String> for LwkError {
145    fn from(msg: String) -> Self {
146        LwkError::Generic { msg }
147    }
148}
149
150impl From<&str> for LwkError {
151    fn from(msg: &str) -> Self {
152        LwkError::Generic {
153            msg: msg.to_owned(),
154        }
155    }
156}
157
158impl<T> From<PoisonError<MutexGuard<'_, T>>> for LwkError {
159    fn from(e: PoisonError<MutexGuard<'_, T>>) -> Self {
160        LwkError::PoisonError { msg: e.to_string() }
161    }
162}
163
164impl From<lwk_common::precision::Error> for LwkError {
165    fn from(value: lwk_common::precision::Error) -> Self {
166        LwkError::Generic {
167            msg: format!("{value:?}"),
168        }
169    }
170}
171
172impl From<elements::bitcoin::secp256k1::Error> for LwkError {
173    fn from(value: elements::bitcoin::secp256k1::Error) -> Self {
174        LwkError::Generic {
175            msg: format!("{value:?}"),
176        }
177    }
178}
179
180impl From<elements::bitcoin::bip32::Error> for LwkError {
181    fn from(value: elements::bitcoin::bip32::Error) -> Self {
182        LwkError::Generic {
183            msg: format!("{value:?}"),
184        }
185    }
186}
187
188impl From<elements::UnblindError> for LwkError {
189    fn from(value: elements::UnblindError) -> Self {
190        LwkError::Generic {
191            msg: format!("{value:?}"),
192        }
193    }
194}
195
196impl From<lwk_wollet::elements_miniscript::psbt::Error> for LwkError {
197    fn from(value: lwk_wollet::elements_miniscript::psbt::Error) -> Self {
198        LwkError::Generic {
199            msg: format!("{value:?}"),
200        }
201    }
202}
203
204impl From<elements::bitcoin::address::ParseError> for LwkError {
205    fn from(value: elements::bitcoin::address::ParseError) -> Self {
206        LwkError::Generic {
207            msg: format!("{value:?}"),
208        }
209    }
210}
211
212#[cfg(feature = "lightning")]
213impl From<lwk_boltz::Error> for LwkError {
214    fn from(value: lwk_boltz::Error) -> Self {
215        match value {
216            lwk_boltz::Error::MagicRoutingHint {
217                address,
218                amount,
219                uri,
220            } => LwkError::MagicRoutingHint {
221                address,
222                amount,
223                uri,
224            },
225            lwk_boltz::Error::Expired { swap_id, status } => {
226                LwkError::SwapExpired { swap_id, status }
227            }
228            lwk_boltz::Error::NoBoltzUpdate => LwkError::NoBoltzUpdate,
229            _ => LwkError::Generic {
230                msg: format!("{value:?}"),
231            },
232        }
233    }
234}