1use crate::ln::script::ShutdownScript;
13
14#[allow(unused_imports)]
15use crate::prelude::*;
16
17use core::fmt;
18
19#[derive(Clone, PartialEq, Eq)]
22pub enum APIError {
23 APIMisuseError {
26 err: String,
28 },
29 FeeRateTooHigh {
33 err: String,
35 feerate: u32,
37 },
38 InvalidRoute {
41 err: String,
43 },
44 ChannelUnavailable {
48 err: String,
50 },
51 MonitorUpdateInProgress,
60 IncompatibleShutdownScript {
69 script: ShutdownScript,
71 },
72}
73
74impl fmt::Debug for APIError {
75 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76 match *self {
77 APIError::APIMisuseError { ref err } => write!(f, "Misuse error: {}", err),
78 APIError::FeeRateTooHigh { ref err, ref feerate } => {
79 write!(f, "{} feerate: {}", err, feerate)
80 },
81 APIError::InvalidRoute { ref err } => write!(f, "Invalid route provided: {}", err),
82 APIError::ChannelUnavailable { ref err } => write!(f, "Channel unavailable: {}", err),
83 APIError::MonitorUpdateInProgress => f.write_str(
84 "Client indicated a channel monitor update is in progress but not yet complete",
85 ),
86 APIError::IncompatibleShutdownScript { ref script } => {
87 write!(f, "Provided a scriptpubkey format not accepted by peer: {}", script)
88 },
89 }
90 }
91}
92
93impl_writeable_tlv_based_enum_upgradable!(APIError,
94 (0, APIMisuseError) => { (0, err, required), },
95 (2, FeeRateTooHigh) => {
96 (0, err, required),
97 (2, feerate, required),
98 },
99 (4, InvalidRoute) => { (0, err, required), },
100 (6, ChannelUnavailable) => { (0, err, required), },
101 (8, MonitorUpdateInProgress) => {},
102 (10, IncompatibleShutdownScript) => { (0, script, required), },
103);
104
105#[inline]
106pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
107 match error_code & 0xff {
108 4 | 5 | 6 => ("sha256_of_onion", 32),
109 11 | 12 => ("htlc_msat", 8),
110 13 | 18 => ("cltv_expiry", 4),
111 19 => ("incoming_htlc_msat", 8),
112 20 => ("flags", 2),
113 _ => ("", 0),
114 }
115}
116
117#[inline]
118pub(crate) fn get_onion_error_description(error_code: u16) -> (&'static str, &'static str) {
119 const BADONION: u16 = 0x8000;
120 const PERM: u16 = 0x4000;
121 const NODE: u16 = 0x2000;
122 const UPDATE: u16 = 0x1000;
123 match error_code {
124 _c if _c == PERM|1 => ("The realm byte was not understood by the processing node", "invalid_realm"),
125 _c if _c == NODE|2 => ("Node indicated temporary node failure", "temporary_node_failure"),
126 _c if _c == PERM|NODE|2 => ("Node indicated permanent node failure", "permanent_node_failure"),
127 _c if _c == PERM|NODE|3 => ("Node indicated the required node feature is missing in the onion", "required_node_feature_missing"),
128 _c if _c == BADONION|PERM|4 => ("Node indicated the version by is not understood", "invalid_onion_version"),
129 _c if _c == BADONION|PERM|5 => ("Node indicated the HMAC of the onion is incorrect", "invalid_onion_hmac"),
130 _c if _c == BADONION|PERM|6 => ("Node indicated the ephemeral public keys is not parseable", "invalid_onion_key"),
131 _c if _c == UPDATE|7 => ("Node indicated the outgoing channel is unable to handle the HTLC temporarily", "temporary_channel_failure"),
132 _c if _c == PERM|8 => ("Node indicated the outgoing channel is unable to handle the HTLC peramanently", "permanent_channel_failure"),
133 _c if _c == PERM|9 => ("Node indicated the required feature for the outgoing channel is not satisfied", "required_channel_feature_missing"),
134 _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"),
135 _c if _c == UPDATE|11 => ("Node indicated the HTLC amount was below the required minmum for the outbound channel", "amount_below_minimum"),
136 _c if _c == UPDATE|12 => ("Node indicated the fee amount does not meet the required level", "fee_insufficient"),
137 _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"),
138 _c if _c == UPDATE|14 => ("Node indicated the CLTV expiry too close to the current block height for safe handling", "expiry_too_soon"),
139 _c if _c == PERM|15 => ("The final node indicated the payment hash is unknown or amount is incorrect", "incorrect_or_unknown_payment_details"),
140 _c if _c == PERM|16 => ("The final node indicated the payment amount is incorrect", "incorrect_payment_amount"),
141 _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"),
142 _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"),
143 _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"),
144 _c if _c == UPDATE|20 => ("Node indicated the outbound channel has been disabled", "channel_disabled"),
145 _c if _c == 21 => ("Node indicated the CLTV expiry in the HTLC is too far in the future", "expiry_too_far"),
146 _c if _c == PERM|22 => ("Node indicated that the decrypted onion per-hop payload was not understood by it or is incomplete", "invalid_onion_payload"),
147 _c if _c == 23 => ("The final node indicated the complete amount of the multi-part payment was not received within a reasonable time", "mpp_timeout"),
148 _ => ("Unknown", ""),
149 }
150}