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: &nativeAPIError) -> Self {
153 nativeAPIError::APIMisuseError {ref err, } => {
154 let mut err_nonref = Clone::clone(err);
155 APIError::APIMisuseError {
156 err: err_nonref.into(),
159 nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
160 let mut err_nonref = Clone::clone(err);
161 let mut feerate_nonref = Clone::clone(feerate);
162 APIError::FeeRateTooHigh {
163 err: err_nonref.into(),
164 feerate: feerate_nonref,
167 nativeAPIError::InvalidRoute {ref err, } => {
168 let mut err_nonref = Clone::clone(err);
169 APIError::InvalidRoute {
170 err: err_nonref.into(),
173 nativeAPIError::ChannelUnavailable {ref err, } => {
174 let mut err_nonref = Clone::clone(err);
175 APIError::ChannelUnavailable {
176 err: err_nonref.into(),
179 nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
180 nativeAPIError::IncompatibleShutdownScript {ref script, } => {
181 let mut script_nonref = Clone::clone(script);
182 APIError::IncompatibleShutdownScript {
183 script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script_nonref), is_owned: true },
189 pub(crate) fn native_into(native: nativeAPIError) -> Self {
191 nativeAPIError::APIMisuseError {mut err, } => {
192 APIError::APIMisuseError {
196 nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
197 APIError::FeeRateTooHigh {
202 nativeAPIError::InvalidRoute {mut err, } => {
203 APIError::InvalidRoute {
207 nativeAPIError::ChannelUnavailable {mut err, } => {
208 APIError::ChannelUnavailable {
212 nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
213 nativeAPIError::IncompatibleShutdownScript {mut script, } => {
214 APIError::IncompatibleShutdownScript {
215 script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script), is_owned: true },
221 /// Frees any resources used by the APIError
223 pub extern "C" fn APIError_free(this_ptr: APIError) { }
224 /// Creates a copy of the APIError
226 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
230 /// Used only if an object of this type is returned as a trait impl by a method
231 pub(crate) extern "C" fn APIError_clone_void(this_ptr: *const c_void) -> *mut c_void {
232 Box::into_raw(Box::new(unsafe { (*(this_ptr as *const APIError)).clone() })) as *mut c_void
235 /// Used only if an object of this type is returned as a trait impl by a method
236 pub(crate) extern "C" fn APIError_free_void(this_ptr: *mut c_void) {
237 let _ = unsafe { Box::from_raw(this_ptr as *mut APIError) };
240 /// Utility method to constructs a new APIMisuseError-variant APIError
241 pub extern "C" fn APIError_apimisuse_error(err: crate::c_types::Str) -> APIError {
242 APIError::APIMisuseError {
247 /// Utility method to constructs a new FeeRateTooHigh-variant APIError
248 pub extern "C" fn APIError_fee_rate_too_high(err: crate::c_types::Str, feerate: u32) -> APIError {
249 APIError::FeeRateTooHigh {
255 /// Utility method to constructs a new InvalidRoute-variant APIError
256 pub extern "C" fn APIError_invalid_route(err: crate::c_types::Str) -> APIError {
257 APIError::InvalidRoute {
262 /// Utility method to constructs a new ChannelUnavailable-variant APIError
263 pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIError {
264 APIError::ChannelUnavailable {
269 /// Utility method to constructs a new MonitorUpdateInProgress-variant APIError
270 pub extern "C" fn APIError_monitor_update_in_progress() -> APIError {
271 APIError::MonitorUpdateInProgress}
273 /// Utility method to constructs a new IncompatibleShutdownScript-variant APIError
274 pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError {
275 APIError::IncompatibleShutdownScript {
279 /// Checks if two APIErrors contain equal inner contents.
280 /// This ignores pointers and is_owned flags and looks at the values in fields.
282 pub extern "C" fn APIError_eq(a: &APIError, b: &APIError) -> bool {
283 if &a.to_native() == &b.to_native() { true } else { false }
286 /// Serialize the APIError object into a byte array which can be read by APIError_read
287 pub extern "C" fn APIError_write(obj: &crate::lightning::util::errors::APIError) -> crate::c_types::derived::CVec_u8Z {
288 crate::c_types::serialize_obj(&unsafe { &*obj }.to_native())
291 pub(crate) extern "C" fn APIError_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
292 APIError_write(unsafe { &*(obj as *const APIError) })
295 /// Read a APIError from a byte array, created by APIError_write
296 pub extern "C" fn APIError_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_COption_APIErrorZDecodeErrorZ {
297 let res: Result<Option<lightning::util::errors::APIError>, lightning::ln::msgs::DecodeError> = crate::c_types::maybe_deserialize_obj(ser);
298 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() };