1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Error types live here.
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};
20 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
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.
29 /// A human-readable error message
30 err: crate::c_types::Str,
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.
36 /// A human-readable error message
37 err: crate::c_types::Str,
38 /// The feerate which was too high.
41 /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
42 /// too-many-hops, etc).
44 /// A human-readable error message
45 err: crate::c_types::Str,
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.
51 /// A human-readable error message
52 err: crate::c_types::Str,
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.
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`].
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).
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,
76 use lightning::util::errors::APIError as APIErrorImport;
77 pub(crate) type nativeAPIError = APIErrorImport;
81 pub(crate) fn to_native(&self) -> nativeAPIError {
83 APIError::APIMisuseError {ref err, } => {
84 let mut err_nonref = Clone::clone(err);
85 nativeAPIError::APIMisuseError {
86 err: err_nonref.into_string(),
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,
97 APIError::InvalidRoute {ref err, } => {
98 let mut err_nonref = Clone::clone(err);
99 nativeAPIError::InvalidRoute {
100 err: err_nonref.into_string(),
103 APIError::ChannelUnavailable {ref err, } => {
104 let mut err_nonref = Clone::clone(err);
105 nativeAPIError::ChannelUnavailable {
106 err: err_nonref.into_string(),
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()) },
119 pub(crate) fn into_native(self) -> nativeAPIError {
121 APIError::APIMisuseError {mut err, } => {
122 nativeAPIError::APIMisuseError {
123 err: err.into_string(),
126 APIError::FeeRateTooHigh {mut err, mut feerate, } => {
127 nativeAPIError::FeeRateTooHigh {
128 err: err.into_string(),
132 APIError::InvalidRoute {mut err, } => {
133 nativeAPIError::InvalidRoute {
134 err: err.into_string(),
137 APIError::ChannelUnavailable {mut err, } => {
138 nativeAPIError::ChannelUnavailable {
139 err: err.into_string(),
142 APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress,
143 APIError::IncompatibleShutdownScript {mut script, } => {
144 nativeAPIError::IncompatibleShutdownScript {
145 script: *unsafe { Box::from_raw(script.take_inner()) },
151 pub(crate) fn from_native(native: &APIErrorImport) -> Self {
152 let native = unsafe { &*(native as *const _ as *const c_void as *const nativeAPIError) };
154 nativeAPIError::APIMisuseError {ref err, } => {
155 let mut err_nonref = Clone::clone(err);
156 APIError::APIMisuseError {
157 err: err_nonref.into(),
160 nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
161 let mut err_nonref = Clone::clone(err);
162 let mut feerate_nonref = Clone::clone(feerate);
163 APIError::FeeRateTooHigh {
164 err: err_nonref.into(),
165 feerate: feerate_nonref,
168 nativeAPIError::InvalidRoute {ref err, } => {
169 let mut err_nonref = Clone::clone(err);
170 APIError::InvalidRoute {
171 err: err_nonref.into(),
174 nativeAPIError::ChannelUnavailable {ref err, } => {
175 let mut err_nonref = Clone::clone(err);
176 APIError::ChannelUnavailable {
177 err: err_nonref.into(),
180 nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
181 nativeAPIError::IncompatibleShutdownScript {ref script, } => {
182 let mut script_nonref = Clone::clone(script);
183 APIError::IncompatibleShutdownScript {
184 script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script_nonref), is_owned: true },
190 pub(crate) fn native_into(native: nativeAPIError) -> Self {
192 nativeAPIError::APIMisuseError {mut err, } => {
193 APIError::APIMisuseError {
197 nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
198 APIError::FeeRateTooHigh {
203 nativeAPIError::InvalidRoute {mut err, } => {
204 APIError::InvalidRoute {
208 nativeAPIError::ChannelUnavailable {mut err, } => {
209 APIError::ChannelUnavailable {
213 nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
214 nativeAPIError::IncompatibleShutdownScript {mut script, } => {
215 APIError::IncompatibleShutdownScript {
216 script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script), is_owned: true },
222 /// Frees any resources used by the APIError
224 pub extern "C" fn APIError_free(this_ptr: APIError) { }
225 /// Creates a copy of the APIError
227 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
231 /// Used only if an object of this type is returned as a trait impl by a method
232 pub(crate) extern "C" fn APIError_clone_void(this_ptr: *const c_void) -> *mut c_void {
233 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const APIError)).clone() })) as *mut c_void
236 /// Used only if an object of this type is returned as a trait impl by a method
237 pub(crate) extern "C" fn APIError_free_void(this_ptr: *mut c_void) {
238 let _ = unsafe { Box::from_raw(this_ptr as *mut APIError) };
241 /// Utility method to constructs a new APIMisuseError-variant APIError
242 pub extern "C" fn APIError_apimisuse_error(err: crate::c_types::Str) -> APIError {
243 APIError::APIMisuseError {
248 /// Utility method to constructs a new FeeRateTooHigh-variant APIError
249 pub extern "C" fn APIError_fee_rate_too_high(err: crate::c_types::Str, feerate: u32) -> APIError {
250 APIError::FeeRateTooHigh {
256 /// Utility method to constructs a new InvalidRoute-variant APIError
257 pub extern "C" fn APIError_invalid_route(err: crate::c_types::Str) -> APIError {
258 APIError::InvalidRoute {
263 /// Utility method to constructs a new ChannelUnavailable-variant APIError
264 pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIError {
265 APIError::ChannelUnavailable {
270 /// Utility method to constructs a new MonitorUpdateInProgress-variant APIError
271 pub extern "C" fn APIError_monitor_update_in_progress() -> APIError {
272 APIError::MonitorUpdateInProgress}
274 /// Utility method to constructs a new IncompatibleShutdownScript-variant APIError
275 pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError {
276 APIError::IncompatibleShutdownScript {
280 /// Checks if two APIErrors contain equal inner contents.
281 /// This ignores pointers and is_owned flags and looks at the values in fields.
283 pub extern "C" fn APIError_eq(a: &APIError, b: &APIError) -> bool {
284 if &a.to_native() == &b.to_native() { true } else { false }
286 /// Get a string which allows debug introspection of a APIError object
287 pub extern "C" fn APIError_debug_str_void(o: *const c_void) -> Str {
288 alloc::format!("{:?}", unsafe { o as *const crate::lightning::util::errors::APIError }).into()}
290 /// Serialize the APIError object into a byte array which can be read by APIError_read
291 pub extern "C" fn APIError_write(obj: &crate::lightning::util::errors::APIError) -> crate::c_types::derived::CVec_u8Z {
292 crate::c_types::serialize_obj(&unsafe { &*obj }.to_native())
295 pub(crate) extern "C" fn APIError_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
296 APIError_write(unsafe { &*(obj as *const APIError) })
299 /// Read a APIError from a byte array, created by APIError_write
300 pub extern "C" fn APIError_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_APIErrorZDecodeErrorZ {
301 let res: Result<Option<lightning::util::errors::APIError>, lightning::ln::msgs::DecodeError> = crate::c_types::maybe_deserialize_obj(ser);
302 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() };