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