08929c15c065c0a98cc382d223cc24417157726b
[ldk-c-bindings] / lightning-c-bindings / src / lightning / util / errors.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! Error types live here.
10
11 use alloc::str::FromStr;
12 use alloc::string::String;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
19
20 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
21 /// too-high values)
22 #[derive(Clone)]
23 #[must_use]
24 #[repr(C)]
25 pub enum APIError {
26         /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
27         /// are documented, but generally indicates some precondition of a function was violated.
28         APIMisuseError {
29                 /// A human-readable error message
30                 err: crate::c_types::Str,
31         },
32         /// Due to a high feerate, we were unable to complete the request.
33         /// For example, this may be returned if the feerate implies we cannot open a channel at the
34         /// requested value, but opening a larger channel would succeed.
35         FeeRateTooHigh {
36                 /// A human-readable error message
37                 err: crate::c_types::Str,
38                 /// The feerate which was too high.
39                 feerate: u32,
40         },
41         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
42         /// too-many-hops, etc).
43         InvalidRoute {
44                 /// A human-readable error message
45                 err: crate::c_types::Str,
46         },
47         /// We were unable to complete the request as the Channel required to do so is unable to
48         /// complete the request (or was not found). This can take many forms, including disconnected
49         /// peer, channel at capacity, channel shutting down, etc.
50         ChannelUnavailable {
51                 /// A human-readable error message
52                 err: crate::c_types::Str,
53         },
54         /// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]
55         /// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a
56         /// monitor update is awaiting async resolution. Once it resolves the attempted action should
57         /// complete automatically.
58         ///
59         /// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel
60         /// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
61         /// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
62         MonitorUpdateInProgress,
63         /// [`SignerProvider::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
64         /// with the channel counterparty as negotiated in [`InitFeatures`].
65         ///
66         /// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open
67         /// a channel or cooperatively close one with this peer (and will have to force-close instead).
68         ///
69         /// [`SignerProvider::get_shutdown_scriptpubkey`]: crate::sign::SignerProvider::get_shutdown_scriptpubkey
70         /// [`InitFeatures`]: crate::ln::features::InitFeatures
71         IncompatibleShutdownScript {
72                 /// The incompatible shutdown script.
73                 script: crate::lightning::ln::script::ShutdownScript,
74         },
75 }
76 use lightning::util::errors::APIError as APIErrorImport;
77 pub(crate) type nativeAPIError = APIErrorImport;
78
79 impl APIError {
80         #[allow(unused)]
81         pub(crate) fn to_native(&self) -> nativeAPIError {
82                 match self {
83                         APIError::APIMisuseError {ref err, } => {
84                                 let mut err_nonref = Clone::clone(err);
85                                 nativeAPIError::APIMisuseError {
86                                         err: err_nonref.into_string(),
87                                 }
88                         },
89                         APIError::FeeRateTooHigh {ref err, ref feerate, } => {
90                                 let mut err_nonref = Clone::clone(err);
91                                 let mut feerate_nonref = Clone::clone(feerate);
92                                 nativeAPIError::FeeRateTooHigh {
93                                         err: err_nonref.into_string(),
94                                         feerate: feerate_nonref,
95                                 }
96                         },
97                         APIError::InvalidRoute {ref err, } => {
98                                 let mut err_nonref = Clone::clone(err);
99                                 nativeAPIError::InvalidRoute {
100                                         err: err_nonref.into_string(),
101                                 }
102                         },
103                         APIError::ChannelUnavailable {ref err, } => {
104                                 let mut err_nonref = Clone::clone(err);
105                                 nativeAPIError::ChannelUnavailable {
106                                         err: err_nonref.into_string(),
107                                 }
108                         },
109                         APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress,
110                         APIError::IncompatibleShutdownScript {ref script, } => {
111                                 let mut script_nonref = Clone::clone(script);
112                                 nativeAPIError::IncompatibleShutdownScript {
113                                         script: *unsafe { Box::from_raw(script_nonref.take_inner()) },
114                                 }
115                         },
116                 }
117         }
118         #[allow(unused)]
119         pub(crate) fn into_native(self) -> nativeAPIError {
120                 match self {
121                         APIError::APIMisuseError {mut err, } => {
122                                 nativeAPIError::APIMisuseError {
123                                         err: err.into_string(),
124                                 }
125                         },
126                         APIError::FeeRateTooHigh {mut err, mut feerate, } => {
127                                 nativeAPIError::FeeRateTooHigh {
128                                         err: err.into_string(),
129                                         feerate: feerate,
130                                 }
131                         },
132                         APIError::InvalidRoute {mut err, } => {
133                                 nativeAPIError::InvalidRoute {
134                                         err: err.into_string(),
135                                 }
136                         },
137                         APIError::ChannelUnavailable {mut err, } => {
138                                 nativeAPIError::ChannelUnavailable {
139                                         err: err.into_string(),
140                                 }
141                         },
142                         APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress,
143                         APIError::IncompatibleShutdownScript {mut script, } => {
144                                 nativeAPIError::IncompatibleShutdownScript {
145                                         script: *unsafe { Box::from_raw(script.take_inner()) },
146                                 }
147                         },
148                 }
149         }
150         #[allow(unused)]
151         pub(crate) fn from_native(native: &nativeAPIError) -> Self {
152                 match native {
153                         nativeAPIError::APIMisuseError {ref err, } => {
154                                 let mut err_nonref = Clone::clone(err);
155                                 APIError::APIMisuseError {
156                                         err: err_nonref.into(),
157                                 }
158                         },
159                         nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
160                                 let mut err_nonref = Clone::clone(err);
161                                 let mut feerate_nonref = Clone::clone(feerate);
162                                 APIError::FeeRateTooHigh {
163                                         err: err_nonref.into(),
164                                         feerate: feerate_nonref,
165                                 }
166                         },
167                         nativeAPIError::InvalidRoute {ref err, } => {
168                                 let mut err_nonref = Clone::clone(err);
169                                 APIError::InvalidRoute {
170                                         err: err_nonref.into(),
171                                 }
172                         },
173                         nativeAPIError::ChannelUnavailable {ref err, } => {
174                                 let mut err_nonref = Clone::clone(err);
175                                 APIError::ChannelUnavailable {
176                                         err: err_nonref.into(),
177                                 }
178                         },
179                         nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
180                         nativeAPIError::IncompatibleShutdownScript {ref script, } => {
181                                 let mut script_nonref = Clone::clone(script);
182                                 APIError::IncompatibleShutdownScript {
183                                         script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script_nonref), is_owned: true },
184                                 }
185                         },
186                 }
187         }
188         #[allow(unused)]
189         pub(crate) fn native_into(native: nativeAPIError) -> Self {
190                 match native {
191                         nativeAPIError::APIMisuseError {mut err, } => {
192                                 APIError::APIMisuseError {
193                                         err: err.into(),
194                                 }
195                         },
196                         nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
197                                 APIError::FeeRateTooHigh {
198                                         err: err.into(),
199                                         feerate: feerate,
200                                 }
201                         },
202                         nativeAPIError::InvalidRoute {mut err, } => {
203                                 APIError::InvalidRoute {
204                                         err: err.into(),
205                                 }
206                         },
207                         nativeAPIError::ChannelUnavailable {mut err, } => {
208                                 APIError::ChannelUnavailable {
209                                         err: err.into(),
210                                 }
211                         },
212                         nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
213                         nativeAPIError::IncompatibleShutdownScript {mut script, } => {
214                                 APIError::IncompatibleShutdownScript {
215                                         script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script), is_owned: true },
216                                 }
217                         },
218                 }
219         }
220 }
221 /// Frees any resources used by the APIError
222 #[no_mangle]
223 pub extern "C" fn APIError_free(this_ptr: APIError) { }
224 /// Creates a copy of the APIError
225 #[no_mangle]
226 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
227         orig.clone()
228 }
229 #[allow(unused)]
230 /// Used only if an object of this type is returned as a trait impl by a method
231 pub(crate) extern "C" fn APIError_clone_void(this_ptr: *const c_void) -> *mut c_void {
232         Box::into_raw(Box::new(unsafe { (*(this_ptr as *const APIError)).clone() })) as *mut c_void
233 }
234 #[allow(unused)]
235 /// Used only if an object of this type is returned as a trait impl by a method
236 pub(crate) extern "C" fn APIError_free_void(this_ptr: *mut c_void) {
237         let _ = unsafe { Box::from_raw(this_ptr as *mut APIError) };
238 }
239 #[no_mangle]
240 /// Utility method to constructs a new APIMisuseError-variant APIError
241 pub extern "C" fn APIError_apimisuse_error(err: crate::c_types::Str) -> APIError {
242         APIError::APIMisuseError {
243                 err,
244         }
245 }
246 #[no_mangle]
247 /// Utility method to constructs a new FeeRateTooHigh-variant APIError
248 pub extern "C" fn APIError_fee_rate_too_high(err: crate::c_types::Str, feerate: u32) -> APIError {
249         APIError::FeeRateTooHigh {
250                 err,
251                 feerate,
252         }
253 }
254 #[no_mangle]
255 /// Utility method to constructs a new InvalidRoute-variant APIError
256 pub extern "C" fn APIError_invalid_route(err: crate::c_types::Str) -> APIError {
257         APIError::InvalidRoute {
258                 err,
259         }
260 }
261 #[no_mangle]
262 /// Utility method to constructs a new ChannelUnavailable-variant APIError
263 pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIError {
264         APIError::ChannelUnavailable {
265                 err,
266         }
267 }
268 #[no_mangle]
269 /// Utility method to constructs a new MonitorUpdateInProgress-variant APIError
270 pub extern "C" fn APIError_monitor_update_in_progress() -> APIError {
271         APIError::MonitorUpdateInProgress}
272 #[no_mangle]
273 /// Utility method to constructs a new IncompatibleShutdownScript-variant APIError
274 pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError {
275         APIError::IncompatibleShutdownScript {
276                 script,
277         }
278 }
279 /// Checks if two APIErrors contain equal inner contents.
280 /// This ignores pointers and is_owned flags and looks at the values in fields.
281 #[no_mangle]
282 pub extern "C" fn APIError_eq(a: &APIError, b: &APIError) -> bool {
283         if &a.to_native() == &b.to_native() { true } else { false }
284 }
285 #[no_mangle]
286 /// Serialize the APIError object into a byte array which can be read by APIError_read
287 pub extern "C" fn APIError_write(obj: &crate::lightning::util::errors::APIError) -> crate::c_types::derived::CVec_u8Z {
288         crate::c_types::serialize_obj(&unsafe { &*obj }.to_native())
289 }
290 #[allow(unused)]
291 pub(crate) extern "C" fn APIError_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
292         APIError_write(unsafe { &*(obj as *const APIError) })
293 }
294 #[no_mangle]
295 /// Read a APIError from a byte array, created by APIError_write
296 pub extern "C" fn APIError_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_APIErrorZDecodeErrorZ {
297         let res: Result<Option<lightning::util::errors::APIError>, lightning::ln::msgs::DecodeError> = crate::c_types::maybe_deserialize_obj(ser);
298         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_res_0 = if o.is_none() { crate::c_types::derived::COption_APIErrorZ::None } else { crate::c_types::derived::COption_APIErrorZ::Some( { crate::lightning::util::errors::APIError::native_into(o.unwrap()) }) }; local_res_0 }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError::native_into(e) }).into() };
299         local_res
300 }