81faa98f461e08b9e4f2091e9cf28229dbf960bd
[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 }
54 use lightning::util::errors::APIError as nativeAPIError;
55 impl APIError {
56         #[allow(unused)]
57         pub(crate) fn to_native(&self) -> nativeAPIError {
58                 match self {
59                         APIError::APIMisuseError {ref err, } => {
60                                 let mut err_nonref = (*err).clone();
61                                 nativeAPIError::APIMisuseError {
62                                         err: err_nonref.into_string(),
63                                 }
64                         },
65                         APIError::FeeRateTooHigh {ref err, ref feerate, } => {
66                                 let mut err_nonref = (*err).clone();
67                                 let mut feerate_nonref = (*feerate).clone();
68                                 nativeAPIError::FeeRateTooHigh {
69                                         err: err_nonref.into_string(),
70                                         feerate: feerate_nonref,
71                                 }
72                         },
73                         APIError::RouteError {ref err, } => {
74                                 let mut err_nonref = (*err).clone();
75                                 nativeAPIError::RouteError {
76                                         err: err_nonref.into_str(),
77                                 }
78                         },
79                         APIError::ChannelUnavailable {ref err, } => {
80                                 let mut err_nonref = (*err).clone();
81                                 nativeAPIError::ChannelUnavailable {
82                                         err: err_nonref.into_string(),
83                                 }
84                         },
85                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
86                 }
87         }
88         #[allow(unused)]
89         pub(crate) fn into_native(self) -> nativeAPIError {
90                 match self {
91                         APIError::APIMisuseError {mut err, } => {
92                                 nativeAPIError::APIMisuseError {
93                                         err: err.into_string(),
94                                 }
95                         },
96                         APIError::FeeRateTooHigh {mut err, mut feerate, } => {
97                                 nativeAPIError::FeeRateTooHigh {
98                                         err: err.into_string(),
99                                         feerate: feerate,
100                                 }
101                         },
102                         APIError::RouteError {mut err, } => {
103                                 nativeAPIError::RouteError {
104                                         err: err.into_str(),
105                                 }
106                         },
107                         APIError::ChannelUnavailable {mut err, } => {
108                                 nativeAPIError::ChannelUnavailable {
109                                         err: err.into_string(),
110                                 }
111                         },
112                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
113                 }
114         }
115         #[allow(unused)]
116         pub(crate) fn from_native(native: &nativeAPIError) -> Self {
117                 match native {
118                         nativeAPIError::APIMisuseError {ref err, } => {
119                                 let mut err_nonref = (*err).clone();
120                                 APIError::APIMisuseError {
121                                         err: err_nonref.into(),
122                                 }
123                         },
124                         nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
125                                 let mut err_nonref = (*err).clone();
126                                 let mut feerate_nonref = (*feerate).clone();
127                                 APIError::FeeRateTooHigh {
128                                         err: err_nonref.into(),
129                                         feerate: feerate_nonref,
130                                 }
131                         },
132                         nativeAPIError::RouteError {ref err, } => {
133                                 let mut err_nonref = (*err).clone();
134                                 APIError::RouteError {
135                                         err: err_nonref.into(),
136                                 }
137                         },
138                         nativeAPIError::ChannelUnavailable {ref err, } => {
139                                 let mut err_nonref = (*err).clone();
140                                 APIError::ChannelUnavailable {
141                                         err: err_nonref.into(),
142                                 }
143                         },
144                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
145                 }
146         }
147         #[allow(unused)]
148         pub(crate) fn native_into(native: nativeAPIError) -> Self {
149                 match native {
150                         nativeAPIError::APIMisuseError {mut err, } => {
151                                 APIError::APIMisuseError {
152                                         err: err.into(),
153                                 }
154                         },
155                         nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
156                                 APIError::FeeRateTooHigh {
157                                         err: err.into(),
158                                         feerate: feerate,
159                                 }
160                         },
161                         nativeAPIError::RouteError {mut err, } => {
162                                 APIError::RouteError {
163                                         err: err.into(),
164                                 }
165                         },
166                         nativeAPIError::ChannelUnavailable {mut err, } => {
167                                 APIError::ChannelUnavailable {
168                                         err: err.into(),
169                                 }
170                         },
171                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
172                 }
173         }
174 }
175 /// Frees any resources used by the APIError
176 #[no_mangle]
177 pub extern "C" fn APIError_free(this_ptr: APIError) { }
178 /// Creates a copy of the APIError
179 #[no_mangle]
180 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
181         orig.clone()
182 }