]> git.bitcoin.ninja Git - rust-lightning/blob - lightning-c-bindings/src/util/errors.rs
Add codecov.yml allowing coverage to drop slightly before "failing"
[rust-lightning] / 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                 err: crate::c_types::derived::CVec_u8Z,
17         },
18         /// Due to a high feerate, we were unable to complete the request.
19         /// For example, this may be returned if the feerate implies we cannot open a channel at the
20         /// requested value, but opening a larger channel would succeed.
21         FeeRateTooHigh {
22                 err: crate::c_types::derived::CVec_u8Z,
23                 feerate: u32,
24         },
25         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
26         /// too-many-hops, etc).
27         RouteError {
28                 err: crate::c_types::Str,
29         },
30         /// We were unable to complete the request as the Channel required to do so is unable to
31         /// complete the request (or was not found). This can take many forms, including disconnected
32         /// peer, channel at capacity, channel shutting down, etc.
33         ChannelUnavailable {
34                 err: crate::c_types::derived::CVec_u8Z,
35         },
36         /// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
37         /// attempted action to fail.
38         MonitorUpdateFailed,
39 }
40 use lightning::util::errors::APIError as nativeAPIError;
41 impl APIError {
42         #[allow(unused)]
43         pub(crate) fn to_native(&self) -> nativeAPIError {
44                 match self {
45                         APIError::APIMisuseError {ref err, } => {
46                                 let mut err_nonref = (*err).clone();
47                                 nativeAPIError::APIMisuseError {
48                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
49                                 }
50                         },
51                         APIError::FeeRateTooHigh {ref err, ref feerate, } => {
52                                 let mut err_nonref = (*err).clone();
53                                 let mut feerate_nonref = (*feerate).clone();
54                                 nativeAPIError::FeeRateTooHigh {
55                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
56                                         feerate: feerate_nonref,
57                                 }
58                         },
59                         APIError::RouteError {ref err, } => {
60                                 let mut err_nonref = (*err).clone();
61                                 nativeAPIError::RouteError {
62                                         err: err_nonref.into(),
63                                 }
64                         },
65                         APIError::ChannelUnavailable {ref err, } => {
66                                 let mut err_nonref = (*err).clone();
67                                 nativeAPIError::ChannelUnavailable {
68                                         err: String::from_utf8(err_nonref.into_rust()).unwrap(),
69                                 }
70                         },
71                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
72                 }
73         }
74         #[allow(unused)]
75         pub(crate) fn into_native(self) -> nativeAPIError {
76                 match self {
77                         APIError::APIMisuseError {mut err, } => {
78                                 nativeAPIError::APIMisuseError {
79                                         err: String::from_utf8(err.into_rust()).unwrap(),
80                                 }
81                         },
82                         APIError::FeeRateTooHigh {mut err, mut feerate, } => {
83                                 nativeAPIError::FeeRateTooHigh {
84                                         err: String::from_utf8(err.into_rust()).unwrap(),
85                                         feerate: feerate,
86                                 }
87                         },
88                         APIError::RouteError {mut err, } => {
89                                 nativeAPIError::RouteError {
90                                         err: err.into(),
91                                 }
92                         },
93                         APIError::ChannelUnavailable {mut err, } => {
94                                 nativeAPIError::ChannelUnavailable {
95                                         err: String::from_utf8(err.into_rust()).unwrap(),
96                                 }
97                         },
98                         APIError::MonitorUpdateFailed => nativeAPIError::MonitorUpdateFailed,
99                 }
100         }
101         #[allow(unused)]
102         pub(crate) fn from_native(native: &nativeAPIError) -> Self {
103                 match native {
104                         nativeAPIError::APIMisuseError {ref err, } => {
105                                 let mut err_nonref = (*err).clone();
106                                 APIError::APIMisuseError {
107                                         err: err_nonref.into_bytes().into(),
108                                 }
109                         },
110                         nativeAPIError::FeeRateTooHigh {ref err, ref feerate, } => {
111                                 let mut err_nonref = (*err).clone();
112                                 let mut feerate_nonref = (*feerate).clone();
113                                 APIError::FeeRateTooHigh {
114                                         err: err_nonref.into_bytes().into(),
115                                         feerate: feerate_nonref,
116                                 }
117                         },
118                         nativeAPIError::RouteError {ref err, } => {
119                                 let mut err_nonref = (*err).clone();
120                                 APIError::RouteError {
121                                         err: err_nonref.into(),
122                                 }
123                         },
124                         nativeAPIError::ChannelUnavailable {ref err, } => {
125                                 let mut err_nonref = (*err).clone();
126                                 APIError::ChannelUnavailable {
127                                         err: err_nonref.into_bytes().into(),
128                                 }
129                         },
130                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
131                 }
132         }
133         #[allow(unused)]
134         pub(crate) fn native_into(native: nativeAPIError) -> Self {
135                 match native {
136                         nativeAPIError::APIMisuseError {mut err, } => {
137                                 APIError::APIMisuseError {
138                                         err: err.into_bytes().into(),
139                                 }
140                         },
141                         nativeAPIError::FeeRateTooHigh {mut err, mut feerate, } => {
142                                 APIError::FeeRateTooHigh {
143                                         err: err.into_bytes().into(),
144                                         feerate: feerate,
145                                 }
146                         },
147                         nativeAPIError::RouteError {mut err, } => {
148                                 APIError::RouteError {
149                                         err: err.into(),
150                                 }
151                         },
152                         nativeAPIError::ChannelUnavailable {mut err, } => {
153                                 APIError::ChannelUnavailable {
154                                         err: err.into_bytes().into(),
155                                 }
156                         },
157                         nativeAPIError::MonitorUpdateFailed => APIError::MonitorUpdateFailed,
158                 }
159         }
160 }
161 #[no_mangle]
162 pub extern "C" fn APIError_free(this_ptr: APIError) { }
163 #[no_mangle]
164 pub extern "C" fn APIError_clone(orig: &APIError) -> APIError {
165         orig.clone()
166 }