Use workspaces to separate crates
[rust-lightning] / lightning / src / util / errors.rs
1 //! Error types live here.
2
3 use std::fmt;
4
5 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
6 /// too-high values)
7 pub enum APIError {
8         /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
9         /// are documented, but generally indicates some precondition of a function was violated.
10         APIMisuseError {
11                 /// A human-readable error message
12                 err: &'static str
13         },
14         /// Due to a high feerate, we were unable to complete the request.
15         /// For example, this may be returned if the feerate implies we cannot open a channel at the
16         /// requested value, but opening a larger channel would succeed.
17         FeeRateTooHigh {
18                 /// A human-readable error message
19                 err: String,
20                 /// The feerate which was too high.
21                 feerate: u64
22         },
23         /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
24         /// too-many-hops, etc).
25         RouteError {
26                 /// A human-readable error message
27                 err: &'static str
28         },
29         /// We were unable to complete the request as the Channel required to do so is unable to
30         /// complete the request (or was not found). This can take many forms, including disconnected
31         /// peer, channel at capacity, channel shutting down, etc.
32         ChannelUnavailable {
33                 /// A human-readable error message
34                 err: &'static str
35         },
36         /// An attempt to call add_update_monitor returned an Err (ie you did this!), causing the
37         /// attempted action to fail.
38         MonitorUpdateFailed,
39 }
40
41 impl fmt::Debug for APIError {
42         fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
43                 match *self {
44                         APIError::APIMisuseError {ref err} => f.write_str(err),
45                         APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
46                         APIError::RouteError {ref err} => f.write_str(err),
47                         APIError::ChannelUnavailable {ref err} => f.write_str(err),
48                         APIError::MonitorUpdateFailed => f.write_str("Client indicated a channel monitor update failed"),
49                 }
50         }
51 }
52
53 #[inline]
54 pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
55         match error_code & 0xff {
56                 4|5|6 => ("sha256_of_onion", 32),
57                 11|12 => ("htlc_msat", 8),
58                 13|18 => ("cltv_expiry", 4),
59                 19 => ("incoming_htlc_msat", 8),
60                 20 => ("flags", 2),
61                 _ => ("", 0),
62         }
63 }
64
65 #[inline]
66 pub(crate) fn get_onion_error_description(error_code: u16) -> (&'static str, &'static str) {
67         const BADONION: u16 = 0x8000;
68         const PERM: u16 = 0x4000;
69         const NODE: u16 = 0x2000;
70         const UPDATE: u16 = 0x1000;
71         match error_code {
72                 _c if _c == PERM|1 => ("The realm byte was not understood by the processing node", "invalid_realm"),
73                 _c if _c == NODE|2 => ("Node indicated temporary node failure", "temporary_node_failure"),
74                 _c if _c == PERM|NODE|2 => ("Node indicated permanent node failure", "permanent_node_failure"),
75                 _c if _c == PERM|NODE|3 => ("Node indicated the required node feature is missing in the onion", "required_node_feature_missing"),
76                 _c if _c == BADONION|PERM|4 => ("Node indicated the version by is not understood", "invalid_onion_version"),
77                 _c if _c == BADONION|PERM|5  => ("Node indicated the HMAC of the onion is incorrect", "invalid_onion_hmac"),
78                 _c if _c == BADONION|PERM|6 => ("Node indicated the ephemeral public keys is not parseable", "invalid_onion_key"),
79                 _c if _c == UPDATE|7 => ("Node indicated the outgoing channel is unable to handle the HTLC temporarily", "temporary_channel_failure"),
80                 _c if _c == PERM|8 => ("Node indicated the outgoing channel is unable to handle the HTLC peramanently", "permanent_channel_failure"),
81                 _c if _c == PERM|9 => ("Node indicated the required feature for the outgoing channel is not satisfied", "required_channel_feature_missing"),
82                 _c if _c == PERM|10 => ("Node indicated the outbound channel is not found for the specified short_channel_id in the onion packet", "unknown_next_peer"),
83                 _c if _c == UPDATE|11 => ("Node indicated the HTLC amount was below the required minmum for the outbound channel", "amount_below_minimum"),
84                 _c if _c == UPDATE|12 => ("Node indicated the fee amount does not meet the required level", "fee_insufficient"),
85                 _c if _c == UPDATE|13 => ("Node indicated the cltv_expiry does not comply with the cltv_expiry_delta required by the outgoing channel", "incorrect_cltv_expiry"),
86                 _c if _c == UPDATE|14 => ("Node indicated the CLTV expiry too close to the current block height for safe handling", "expiry_too_soon"),
87                 _c if _c == PERM|15 => ("The final node indicated the payment hash is unknown or amount is incorrect", "incorrect_or_unknown_payment_details"),
88                 _c if _c == PERM|16 => ("The final node indicated the payment amount is incorrect", "incorrect_payment_amount"),
89                 _c if _c == 17 => ("The final node indicated the CLTV expiry is too close to the current block height for safe handling", "final_expiry_too_soon"),
90                 _c if _c == 18 => ("The final node indicated the CLTV expiry in the HTLC does not match the value in the onion", "final_incorrect_cltv_expiry"),
91                 _c if _c == 19 => ("The final node indicated the amount in the HTLC does not match the value in the onion", "final_incorrect_htlc_amount"),
92                 _c if _c == UPDATE|20 => ("Node indicated the outbound channel has been disabled", "channel_disabled"),
93                 _c if _c == 21 => ("Node indicated the CLTV expiry in the HTLC is too far in the future", "expiry_too_far"),
94                 _ => ("Unknown", ""),
95         }
96 }