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 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};
19 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
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.
28 /// A human-readable error message
29 err: crate::c_types::Str,
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.
35 /// A human-readable error message
36 err: crate::c_types::Str,
37 /// The feerate which was too high.
40 /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
41 /// too-many-hops, etc).
43 /// A human-readable error message
44 err: crate::c_types::Str,
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.
50 /// A human-readable error message
51 err: crate::c_types::Str,
53 /// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]
54 /// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a
55 /// monitor update is awaiting async resolution. Once it resolves the attempted action should
56 /// complete automatically.
58 /// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel
59 /// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
60 /// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
61 MonitorUpdateInProgress,
62 /// [`KeysInterface::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
63 /// with the channel counterparty as negotiated in [`InitFeatures`].
65 /// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open
66 /// a channel or cooperatively close one with this peer (and will have to force-close instead).
68 /// [`KeysInterface::get_shutdown_scriptpubkey`]: crate::chain::keysinterface::KeysInterface::get_shutdown_scriptpubkey
69 /// [`InitFeatures`]: crate::ln::features::InitFeatures
70 IncompatibleShutdownScript {
71 /// The incompatible shutdown script.
72 script: crate::lightning::ln::script::ShutdownScript,
75 use lightning::util::errors::APIError as APIErrorImport;
76 pub(crate) type nativeAPIError = APIErrorImport;
80 pub(crate) fn to_native(&self) -> nativeAPIError {
82 APIError::APIMisuseError {ref err, } => {
83 let mut err_nonref = Clone::clone(err);
84 nativeAPIError::APIMisuseError {
85 err: err_nonref.into_string(),
88 APIError::FeeRateTooHigh {ref err, ref feerate, } => {
89 let mut err_nonref = Clone::clone(err);
90 let mut feerate_nonref = Clone::clone(feerate);
91 nativeAPIError::FeeRateTooHigh {
92 err: err_nonref.into_string(),
93 feerate: feerate_nonref,
96 APIError::InvalidRoute {ref err, } => {
97 let mut err_nonref = Clone::clone(err);
98 nativeAPIError::InvalidRoute {
99 err: err_nonref.into_str(),
102 APIError::ChannelUnavailable {ref err, } => {
103 let mut err_nonref = Clone::clone(err);
104 nativeAPIError::ChannelUnavailable {
105 err: err_nonref.into_string(),
108 APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress,
109 APIError::IncompatibleShutdownScript {ref script, } => {
110 let mut script_nonref = Clone::clone(script);
111 nativeAPIError::IncompatibleShutdownScript {
112 script: *unsafe { Box::from_raw(script_nonref.take_inner()) },
118 pub(crate) fn into_native(self) -> nativeAPIError {
120 APIError::APIMisuseError {mut err, } => {
121 nativeAPIError::APIMisuseError {
122 err: err.into_string(),
125 APIError::FeeRateTooHigh {mut err, mut feerate, } => {
126 nativeAPIError::FeeRateTooHigh {
127 err: err.into_string(),
131 APIError::InvalidRoute {mut err, } => {
132 nativeAPIError::InvalidRoute {
136 APIError::ChannelUnavailable {mut err, } => {
137 nativeAPIError::ChannelUnavailable {
138 err: err.into_string(),
141 APIError::MonitorUpdateInProgress => nativeAPIError::MonitorUpdateInProgress,
142 APIError::IncompatibleShutdownScript {mut script, } => {
143 nativeAPIError::IncompatibleShutdownScript {
144 script: *unsafe { Box::from_raw(script.take_inner()) },
150 pub(crate) fn from_native(native: &nativeAPIError) -> Self {
152 nativeAPIError::APIMisuseError {ref err, } => {
153 let mut err_nonref = Clone::clone(err);
154 APIError::APIMisuseError {
155 err: err_nonref.into(),
158 nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
159 let mut err_nonref = Clone::clone(err);
160 let mut feerate_nonref = Clone::clone(feerate);
161 APIError::FeeRateTooHigh {
162 err: err_nonref.into(),
163 feerate: feerate_nonref,
166 nativeAPIError::InvalidRoute {ref err, } => {
167 let mut err_nonref = Clone::clone(err);
168 APIError::InvalidRoute {
169 err: err_nonref.into(),
172 nativeAPIError::ChannelUnavailable {ref err, } => {
173 let mut err_nonref = Clone::clone(err);
174 APIError::ChannelUnavailable {
175 err: err_nonref.into(),
178 nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
179 nativeAPIError::IncompatibleShutdownScript {ref script, } => {
180 let mut script_nonref = Clone::clone(script);
181 APIError::IncompatibleShutdownScript {
182 script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script_nonref), is_owned: true },
188 pub(crate) fn native_into(native: nativeAPIError) -> Self {
190 nativeAPIError::APIMisuseError {mut err, } => {
191 APIError::APIMisuseError {
195 nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
196 APIError::FeeRateTooHigh {
201 nativeAPIError::InvalidRoute {mut err, } => {
202 APIError::InvalidRoute {
206 nativeAPIError::ChannelUnavailable {mut err, } => {
207 APIError::ChannelUnavailable {
211 nativeAPIError::MonitorUpdateInProgress => APIError::MonitorUpdateInProgress,
212 nativeAPIError::IncompatibleShutdownScript {mut script, } => {
213 APIError::IncompatibleShutdownScript {
214 script: crate::lightning::ln::script::ShutdownScript { inner: ObjOps::heap_alloc(script), is_owned: true },
220 /// Frees any resources used by the APIError
222 pub extern "C" fn APIError_free(this_ptr: APIError) { }
223 /// Creates a copy of the APIError
225 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
229 /// Utility method to constructs a new APIMisuseError-variant APIError
230 pub extern "C" fn APIError_apimisuse_error(err: crate::c_types::Str) -> APIError {
231 APIError::APIMisuseError {
236 /// Utility method to constructs a new FeeRateTooHigh-variant APIError
237 pub extern "C" fn APIError_fee_rate_too_high(err: crate::c_types::Str, feerate: u32) -> APIError {
238 APIError::FeeRateTooHigh {
244 /// Utility method to constructs a new InvalidRoute-variant APIError
245 pub extern "C" fn APIError_invalid_route(err: crate::c_types::Str) -> APIError {
246 APIError::InvalidRoute {
251 /// Utility method to constructs a new ChannelUnavailable-variant APIError
252 pub extern "C" fn APIError_channel_unavailable(err: crate::c_types::Str) -> APIError {
253 APIError::ChannelUnavailable {
258 /// Utility method to constructs a new MonitorUpdateInProgress-variant APIError
259 pub extern "C" fn APIError_monitor_update_in_progress() -> APIError {
260 APIError::MonitorUpdateInProgress}
262 /// Utility method to constructs a new IncompatibleShutdownScript-variant APIError
263 pub extern "C" fn APIError_incompatible_shutdown_script(script: crate::lightning::ln::script::ShutdownScript) -> APIError {
264 APIError::IncompatibleShutdownScript {
268 /// Checks if two APIErrors contain equal inner contents.
269 /// This ignores pointers and is_owned flags and looks at the values in fields.
271 pub extern "C" fn APIError_eq(a: &APIError, b: &APIError) -> bool {
272 if &a.to_native() == &b.to_native() { true } else { false }