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