Update auto-generated bindings
[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 core::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16 #[cfg(feature="no-std")]
17 use alloc::{vec::Vec, boxed::Box};
18
19 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
20 /// too-high values)
21 #[must_use]
22 #[derive(Clone)]
23 #[repr(C)]
24 pub enum APIError {
25         /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
26         /// are documented, but generally indicates some precondition of a function was violated.
27         APIMisuseError {
28                 /// A human-readable error message
29                 err: crate::c_types::Str,
30         },
31         /// Due to a high feerate, we were unable to complete the request.
32         /// For example, this may be returned if the feerate implies we cannot open a channel at the
33         /// requested value, but opening a larger channel would succeed.
34         FeeRateTooHigh {
35                 /// A human-readable error message
36                 err: crate::c_types::Str,
37                 /// The feerate which was too high.
38                 feerate: u32,
39         },
40         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
41         /// too-many-hops, etc).
42         RouteError {
43                 /// A human-readable error message
44                 err: crate::c_types::Str,
45         },
46         /// We were unable to complete the request as the Channel required to do so is unable to
47         /// complete the request (or was not found). This can take many forms, including disconnected
48         /// peer, channel at capacity, channel shutting down, etc.
49         ChannelUnavailable {
50                 /// A human-readable error message
51                 err: crate::c_types::Str,
52         },
53         /// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
54         /// attempted action to fail.
55         MonitorUpdateFailed,
56         /// [`KeysInterface::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
57         /// with the channel counterparty as negotiated in [`InitFeatures`].
58         ///
59         /// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open
60         /// a channel or cooperatively close one with this peer (and will have to force-close instead).
61         ///
62         /// [`KeysInterface::get_shutdown_scriptpubkey`]: crate::chain::keysinterface::KeysInterface::get_shutdown_scriptpubkey
63         /// [`InitFeatures`]: crate::ln::features::InitFeatures
64         IncompatibleShutdownScript {
65                 /// The incompatible shutdown script.
66                 script: crate::lightning::ln::script::ShutdownScript,
67         },
68 }
69 use lightning::util::errors::APIError as nativeAPIError;
70 impl APIError {
71         #[allow(unused)]
72         pub(crate) fn to_native(&self) -> nativeAPIError {
73                 match self {
74                         APIError::APIMisuseError {ref err, } => {
75                                 let mut err_nonref = (*err).clone();
76                                 nativeAPIError::APIMisuseError {
77                                         err: err_nonref.into_string(),
78                                 }
79                         },
80                         APIError::FeeRateTooHigh {ref err, ref feerate, } => {
81                                 let mut err_nonref = (*err).clone();
82                                 let mut feerate_nonref = (*feerate).clone();
83                                 nativeAPIError::FeeRateTooHigh {
84                                         err: err_nonref.into_string(),
85                                         feerate: feerate_nonref,
86                                 }
87                         },
88                         APIError::RouteError {ref err, } => {
89                                 let mut err_nonref = (*err).clone();
90                                 nativeAPIError::RouteError {
91                                         err: err_nonref.into_str(),
92                                 }
93                         },
94                         APIError::ChannelUnavailable {ref err, } => {
95                                 let mut err_nonref = (*err).clone();
96                                 nativeAPIError::ChannelUnavailable {
97                                         err: err_nonref.into_string(),
98                                 }
99                         },
100                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
101                         APIError::IncompatibleShutdownScript {ref script, } => {
102                                 let mut script_nonref = (*script).clone();
103                                 nativeAPIError::IncompatibleShutdownScript {
104                                         script: *unsafe { Box::from_raw(script_nonref.take_inner()) },
105                                 }
106                         },
107                 }
108         }
109         #[allow(unused)]
110         pub(crate) fn into_native(self) -> nativeAPIError {
111                 match self {
112                         APIError::APIMisuseError {mut err, } => {
113                                 nativeAPIError::APIMisuseError {
114                                         err: err.into_string(),
115                                 }
116                         },
117                         APIError::FeeRateTooHigh {mut err, mut feerate, } => {
118                                 nativeAPIError::FeeRateTooHigh {
119                                         err: err.into_string(),
120                                         feerate: feerate,
121                                 }
122                         },
123                         APIError::RouteError {mut err, } => {
124                                 nativeAPIError::RouteError {
125                                         err: err.into_str(),
126                                 }
127                         },
128                         APIError::ChannelUnavailable {mut err, } => {
129                                 nativeAPIError::ChannelUnavailable {
130                                         err: err.into_string(),
131                                 }
132                         },
133                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
134                         APIError::IncompatibleShutdownScript {mut script, } => {
135                                 nativeAPIError::IncompatibleShutdownScript {
136                                         script: *unsafe { Box::from_raw(script.take_inner()) },
137                                 }
138                         },
139                 }
140         }
141         #[allow(unused)]
142         pub(crate) fn from_native(native: &nativeAPIError) -> Self {
143                 match native {
144                         nativeAPIError::APIMisuseError {ref err, } => {
145                                 let mut err_nonref = (*err).clone();
146                                 APIError::APIMisuseError {
147                                         err: err_nonref.into(),
148                                 }
149                         },
150                         nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
151                                 let mut err_nonref = (*err).clone();
152                                 let mut feerate_nonref = (*feerate).clone();
153                                 APIError::FeeRateTooHigh {
154                                         err: err_nonref.into(),
155                                         feerate: feerate_nonref,
156                                 }
157                         },
158                         nativeAPIError::RouteError {ref err, } => {
159                                 let mut err_nonref = (*err).clone();
160                                 APIError::RouteError {
161                                         err: err_nonref.into(),
162                                 }
163                         },
164                         nativeAPIError::ChannelUnavailable {ref err, } => {
165                                 let mut err_nonref = (*err).clone();
166                                 APIError::ChannelUnavailable {
167                                         err: err_nonref.into(),
168                                 }
169                         },
170                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
171                         nativeAPIError::IncompatibleShutdownScript {ref script, } => {
172                                 let mut script_nonref = (*script).clone();
173                                 APIError::IncompatibleShutdownScript {
174                                         script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script_nonref), is_owned: true },
175                                 }
176                         },
177                 }
178         }
179         #[allow(unused)]
180         pub(crate) fn native_into(native: nativeAPIError) -> Self {
181                 match native {
182                         nativeAPIError::APIMisuseError {mut err, } => {
183                                 APIError::APIMisuseError {
184                                         err: err.into(),
185                                 }
186                         },
187                         nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
188                                 APIError::FeeRateTooHigh {
189                                         err: err.into(),
190                                         feerate: feerate,
191                                 }
192                         },
193                         nativeAPIError::RouteError {mut err, } => {
194                                 APIError::RouteError {
195                                         err: err.into(),
196                                 }
197                         },
198                         nativeAPIError::ChannelUnavailable {mut err, } => {
199                                 APIError::ChannelUnavailable {
200                                         err: err.into(),
201                                 }
202                         },
203                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
204                         nativeAPIError::IncompatibleShutdownScript {mut script, } => {
205                                 APIError::IncompatibleShutdownScript {
206                                         script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script), is_owned: true },
207                                 }
208                         },
209                 }
210         }
211 }
212 /// Frees any resources used by the APIError
213 #[no_mangle]
214 pub extern "C" fn APIError_free(this_ptr: APIError) { }
215 /// Creates a copy of the APIError
216 #[no_mangle]
217 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
218         orig.clone()
219 }
220 #[no_mangle]
221 /// Utility method to constructs a new APIMisuseError-variant APIError
222 pub extern "C" fn APIError_apimisuse_error(err: crate::c_types::Str) -> APIError {
223         APIError::APIMisuseError {
224                 err,
225         }
226 }
227 #[no_mangle]
228 /// Utility method to constructs a new FeeRateTooHigh-variant APIError
229 pub extern "C" fn APIError_fee_rate_too_high(err: crate::c_types::Str, feerate: u32) -> APIError {
230         APIError::FeeRateTooHigh {
231                 err,
232                 feerate,
233         }
234 }
235 #[no_mangle]
236 /// Utility method to constructs a new RouteError-variant APIError
237 pub extern "C" fn APIError_route_error(err: crate::c_types::Str) -> APIError {
238         APIError::RouteError {
239                 err,
240         }
241 }
242 #[no_mangle]
243 /// Utility method to constructs a new ChannelUnavailable-variant APIError
244 pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIError {
245         APIError::ChannelUnavailable {
246                 err,
247         }
248 }
249 #[no_mangle]
250 /// Utility method to constructs a new MonitorUpdateFailed-variant APIError
251 pub extern "C" fn APIError_monitor_update_failed() -> APIError {
252         APIError::MonitorUpdateFailed}
253 #[no_mangle]
254 /// Utility method to constructs a new IncompatibleShutdownScript-variant APIError
255 pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError {
256         APIError::IncompatibleShutdownScript {
257                 script,
258         }
259 }