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