Rebuild against current RL main
[ldk-c-bindings] / lightning-c-bindings / src / util / errors.rs
1 //! Error types live here.
2
3 use std::ffi::c_void;
4 use bitcoin::hashes::Hash;
5 use crate::c_types::*;
6
7 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
8 /// too-high values)
9 #[must_use]
10 #[derive(Clone)]
11 #[repr(C)]
12 pub enum APIError {
13         /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
14         /// are documented, but generally indicates some precondition of a function was violated.
15         APIMisuseError {
16                 /// A human-readable error message
17                 err: crate::c_types::derived::CVec_u8Z,
18         },
19         /// Due to a high feerate, we were unable to complete the request.
20         /// For example, this may be returned if the feerate implies we cannot open a channel at the
21         /// requested value, but opening a larger channel would succeed.
22         FeeRateTooHigh {
23                 /// A human-readable error message
24                 err: crate::c_types::derived::CVec_u8Z,
25                 /// The feerate which was too high.
26                 feerate: u32,
27         },
28         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
29         /// too-many-hops, etc).
30         RouteError {
31                 /// A human-readable error message
32                 err: crate::c_types::Str,
33         },
34         /// We were unable to complete the request as the Channel required to do so is unable to
35         /// complete the request (or was not found). This can take many forms, including disconnected
36         /// peer, channel at capacity, channel shutting down, etc.
37         ChannelUnavailable {
38                 /// A human-readable error message
39                 err: crate::c_types::derived::CVec_u8Z,
40         },
41         /// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
42         /// attempted action to fail.
43         MonitorUpdateFailed,
44 }
45 use lightning::util::errors::APIError as nativeAPIError;
46 impl APIError {
47         #[allow(unused)]
48         pub(crate) fn to_native(&self) -> nativeAPIError {
49                 match self {
50                         APIError::APIMisuseError {ref err, } => {
51                                 let mut err_nonref = (*err).clone();
52                                 nativeAPIError::APIMisuseError {
53                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
54                                 }
55                         },
56                         APIError::FeeRateTooHigh {ref err, ref feerate, } => {
57                                 let mut err_nonref = (*err).clone();
58                                 let mut feerate_nonref = (*feerate).clone();
59                                 nativeAPIError::FeeRateTooHigh {
60                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
61                                         feerate: feerate_nonref,
62                                 }
63                         },
64                         APIError::RouteError {ref err, } => {
65                                 let mut err_nonref = (*err).clone();
66                                 nativeAPIError::RouteError {
67                                         err: err_nonref.into(),
68                                 }
69                         },
70                         APIError::ChannelUnavailable {ref err, } => {
71                                 let mut err_nonref = (*err).clone();
72                                 nativeAPIError::ChannelUnavailable {
73                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
74                                 }
75                         },
76                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
77                 }
78         }
79         #[allow(unused)]
80         pub(crate) fn into_native(self) -> nativeAPIError {
81                 match self {
82                         APIError::APIMisuseError {mut err, } => {
83                                 nativeAPIError::APIMisuseError {
84                                         err: String::from_utf8(err.into_rust()).unwrap(),
85                                 }
86                         },
87                         APIError::FeeRateTooHigh {mut err, mut feerate, } => {
88                                 nativeAPIError::FeeRateTooHigh {
89                                         err: String::from_utf8(err.into_rust()).unwrap(),
90                                         feerate: feerate,
91                                 }
92                         },
93                         APIError::RouteError {mut err, } => {
94                                 nativeAPIError::RouteError {
95                                         err: err.into(),
96                                 }
97                         },
98                         APIError::ChannelUnavailable {mut err, } => {
99                                 nativeAPIError::ChannelUnavailable {
100                                         err: String::from_utf8(err.into_rust()).unwrap(),
101                                 }
102                         },
103                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
104                 }
105         }
106         #[allow(unused)]
107         pub(crate) fn from_native(native: &nativeAPIError) -> Self {
108                 match native {
109                         nativeAPIError::APIMisuseError {ref err, } => {
110                                 let mut err_nonref = (*err).clone();
111                                 APIError::APIMisuseError {
112                                         err: err_nonref.into_bytes().into(),
113                                 }
114                         },
115                         nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
116                                 let mut err_nonref = (*err).clone();
117                                 let mut feerate_nonref = (*feerate).clone();
118                                 APIError::FeeRateTooHigh {
119                                         err: err_nonref.into_bytes().into(),
120                                         feerate: feerate_nonref,
121                                 }
122                         },
123                         nativeAPIError::RouteError {ref err, } => {
124                                 let mut err_nonref = (*err).clone();
125                                 APIError::RouteError {
126                                         err: err_nonref.into(),
127                                 }
128                         },
129                         nativeAPIError::ChannelUnavailable {ref err, } => {
130                                 let mut err_nonref = (*err).clone();
131                                 APIError::ChannelUnavailable {
132                                         err: err_nonref.into_bytes().into(),
133                                 }
134                         },
135                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
136                 }
137         }
138         #[allow(unused)]
139         pub(crate) fn native_into(native: nativeAPIError) -> Self {
140                 match native {
141                         nativeAPIError::APIMisuseError {mut err, } => {
142                                 APIError::APIMisuseError {
143                                         err: err.into_bytes().into(),
144                                 }
145                         },
146                         nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
147                                 APIError::FeeRateTooHigh {
148                                         err: err.into_bytes().into(),
149                                         feerate: feerate,
150                                 }
151                         },
152                         nativeAPIError::RouteError {mut err, } => {
153                                 APIError::RouteError {
154                                         err: err.into(),
155                                 }
156                         },
157                         nativeAPIError::ChannelUnavailable {mut err, } => {
158                                 APIError::ChannelUnavailable {
159                                         err: err.into_bytes().into(),
160                                 }
161                         },
162                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
163                 }
164         }
165 }
166 /// Frees any resources used by the APIError
167 #[no_mangle]
168 pub extern "C" fn APIError_free(this_ptr: APIError) { }
169 /// Creates a copy of the APIError
170 #[no_mangle]
171 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
172         orig.clone()
173 }