fffe44fe462440b66338c7ccab1273ac68b3294b
[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 #[derive(Clone)]
22 #[must_use]
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 APIErrorImport;
70 pub(crate) type nativeAPIError = APIErrorImport;
71
72 impl APIError {
73         #[allow(unused)]
74         pub(crate) fn to_native(&self) -> nativeAPIError {
75                 match self {
76                         APIError::APIMisuseError {ref err, } => {
77                                 let mut err_nonref = (*err).clone();
78                                 nativeAPIError::APIMisuseError {
79                                         err: err_nonref.into_string(),
80                                 }
81                         },
82                         APIError::FeeRateTooHigh {ref err, ref feerate, } => {
83                                 let mut err_nonref = (*err).clone();
84                                 let mut feerate_nonref = (*feerate).clone();
85                                 nativeAPIError::FeeRateTooHigh {
86                                         err: err_nonref.into_string(),
87                                         feerate: feerate_nonref,
88                                 }
89                         },
90                         APIError::RouteError {ref err, } => {
91                                 let mut err_nonref = (*err).clone();
92                                 nativeAPIError::RouteError {
93                                         err: err_nonref.into_str(),
94                                 }
95                         },
96                         APIError::ChannelUnavailable {ref err, } => {
97                                 let mut err_nonref = (*err).clone();
98                                 nativeAPIError::ChannelUnavailable {
99                                         err: err_nonref.into_string(),
100                                 }
101                         },
102                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
103                         APIError::IncompatibleShutdownScript {ref script, } => {
104                                 let mut script_nonref = (*script).clone();
105                                 nativeAPIError::IncompatibleShutdownScript {
106                                         script: *unsafe { Box::from_raw(script_nonref.take_inner()) },
107                                 }
108                         },
109                 }
110         }
111         #[allow(unused)]
112         pub(crate) fn into_native(self) -> nativeAPIError {
113                 match self {
114                         APIError::APIMisuseError {mut err, } => {
115                                 nativeAPIError::APIMisuseError {
116                                         err: err.into_string(),
117                                 }
118                         },
119                         APIError::FeeRateTooHigh {mut err, mut feerate, } => {
120                                 nativeAPIError::FeeRateTooHigh {
121                                         err: err.into_string(),
122                                         feerate: feerate,
123                                 }
124                         },
125                         APIError::RouteError {mut err, } => {
126                                 nativeAPIError::RouteError {
127                                         err: err.into_str(),
128                                 }
129                         },
130                         APIError::ChannelUnavailable {mut err, } => {
131                                 nativeAPIError::ChannelUnavailable {
132                                         err: err.into_string(),
133                                 }
134                         },
135                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
136                         APIError::IncompatibleShutdownScript {mut script, } => {
137                                 nativeAPIError::IncompatibleShutdownScript {
138                                         script: *unsafe { Box::from_raw(script.take_inner()) },
139                                 }
140                         },
141                 }
142         }
143         #[allow(unused)]
144         pub(crate) fn from_native(native: &nativeAPIError) -> Self {
145                 match native {
146                         nativeAPIError::APIMisuseError {ref err, } => {
147                                 let mut err_nonref = (*err).clone();
148                                 APIError::APIMisuseError {
149                                         err: err_nonref.into(),
150                                 }
151                         },
152                         nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
153                                 let mut err_nonref = (*err).clone();
154                                 let mut feerate_nonref = (*feerate).clone();
155                                 APIError::FeeRateTooHigh {
156                                         err: err_nonref.into(),
157                                         feerate: feerate_nonref,
158                                 }
159                         },
160                         nativeAPIError::RouteError {ref err, } => {
161                                 let mut err_nonref = (*err).clone();
162                                 APIError::RouteError {
163                                         err: err_nonref.into(),
164                                 }
165                         },
166                         nativeAPIError::ChannelUnavailable {ref err, } => {
167                                 let mut err_nonref = (*err).clone();
168                                 APIError::ChannelUnavailable {
169                                         err: err_nonref.into(),
170                                 }
171                         },
172                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
173                         nativeAPIError::IncompatibleShutdownScript {ref script, } => {
174                                 let mut script_nonref = (*script).clone();
175                                 APIError::IncompatibleShutdownScript {
176                                         script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script_nonref), is_owned: true },
177                                 }
178                         },
179                 }
180         }
181         #[allow(unused)]
182         pub(crate) fn native_into(native: nativeAPIError) -> Self {
183                 match native {
184                         nativeAPIError::APIMisuseError {mut err, } => {
185                                 APIError::APIMisuseError {
186                                         err: err.into(),
187                                 }
188                         },
189                         nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
190                                 APIError::FeeRateTooHigh {
191                                         err: err.into(),
192                                         feerate: feerate,
193                                 }
194                         },
195                         nativeAPIError::RouteError {mut err, } => {
196                                 APIError::RouteError {
197                                         err: err.into(),
198                                 }
199                         },
200                         nativeAPIError::ChannelUnavailable {mut err, } => {
201                                 APIError::ChannelUnavailable {
202                                         err: err.into(),
203                                 }
204                         },
205                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
206                         nativeAPIError::IncompatibleShutdownScript {mut script, } => {
207                                 APIError::IncompatibleShutdownScript {
208                                         script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script), is_owned: true },
209                                 }
210                         },
211                 }
212         }
213 }
214 /// Frees any resources used by the APIError
215 #[no_mangle]
216 pub extern "C" fn APIError_free(this_ptr: APIError) { }
217 /// Creates a copy of the APIError
218 #[no_mangle]
219 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
220         orig.clone()
221 }
222 #[no_mangle]
223 /// Utility method to constructs a new APIMisuseError-variant APIError
224 pub extern "C" fn APIError_apimisuse_error(err: crate::c_types::Str) -> APIError {
225         APIError::APIMisuseError {
226                 err,
227         }
228 }
229 #[no_mangle]
230 /// Utility method to constructs a new FeeRateTooHigh-variant APIError
231 pub extern "C" fn APIError_fee_rate_too_high(err: crate::c_types::Str, feerate: u32) -> APIError {
232         APIError::FeeRateTooHigh {
233                 err,
234                 feerate,
235         }
236 }
237 #[no_mangle]
238 /// Utility method to constructs a new RouteError-variant APIError
239 pub extern "C" fn APIError_route_error(err: crate::c_types::Str) -> APIError {
240         APIError::RouteError {
241                 err,
242         }
243 }
244 #[no_mangle]
245 /// Utility method to constructs a new ChannelUnavailable-variant APIError
246 pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIError {
247         APIError::ChannelUnavailable {
248                 err,
249         }
250 }
251 #[no_mangle]
252 /// Utility method to constructs a new MonitorUpdateFailed-variant APIError
253 pub extern "C" fn APIError_monitor_update_failed() -> APIError {
254         APIError::MonitorUpdateFailed}
255 #[no_mangle]
256 /// Utility method to constructs a new IncompatibleShutdownScript-variant APIError
257 pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError {
258         APIError::IncompatibleShutdownScript {
259                 script,
260         }
261 }