Use `crate::prelude::*` rather than specific imports
[rust-lightning] / lightning / src / util / errors.rs
index 6495d9de4ed5363fb4a2b70c958366653797121c..735ce044f810b6a4f9aa97e500c1c628cd43ee4d 100644 (file)
@@ -9,14 +9,16 @@
 
 //! Error types live here.
 
-use ln::script::ShutdownScript;
+use crate::ln::script::ShutdownScript;
+
+#[allow(unused_imports)]
+use crate::prelude::*;
 
-use alloc::string::String;
 use core::fmt;
 
 /// Indicates an error on the client's part (usually some variant of attempting to use too-low or
 /// too-high values)
-#[derive(Clone, PartialEq)]
+#[derive(Clone, PartialEq, Eq)]
 pub enum APIError {
        /// Indicates the API was wholly misused (see err for more). Cases where these can be returned
        /// are documented, but generally indicates some precondition of a function was violated.
@@ -35,9 +37,9 @@ pub enum APIError {
        },
        /// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
        /// too-many-hops, etc).
-       RouteError {
+       InvalidRoute {
                /// A human-readable error message
-               err: &'static str
+               err: String
        },
        /// We were unable to complete the request as the Channel required to do so is unable to
        /// complete the request (or was not found). This can take many forms, including disconnected
@@ -46,16 +48,22 @@ pub enum APIError {
                /// A human-readable error message
                err: String
        },
-       /// An attempt to call watch/update_channel returned an Err (ie you did this!), causing the
-       /// attempted action to fail.
-       MonitorUpdateFailed,
-       /// [`KeysInterface::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
+       /// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]
+       /// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a
+       /// monitor update is awaiting async resolution. Once it resolves the attempted action should
+       /// complete automatically.
+       ///
+       /// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel
+       /// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
+       /// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
+       MonitorUpdateInProgress,
+       /// [`SignerProvider::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
        /// with the channel counterparty as negotiated in [`InitFeatures`].
        ///
        /// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open
        /// a channel or cooperatively close one with this peer (and will have to force-close instead).
        ///
-       /// [`KeysInterface::get_shutdown_scriptpubkey`]: crate::chain::keysinterface::KeysInterface::get_shutdown_scriptpubkey
+       /// [`SignerProvider::get_shutdown_scriptpubkey`]: crate::sign::SignerProvider::get_shutdown_scriptpubkey
        /// [`InitFeatures`]: crate::ln::features::InitFeatures
        IncompatibleShutdownScript {
                /// The incompatible shutdown script.
@@ -66,11 +74,11 @@ pub enum APIError {
 impl fmt::Debug for APIError {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
                match *self {
-                       APIError::APIMisuseError {ref err} => f.write_str(err),
+                       APIError::APIMisuseError {ref err} => write!(f, "Misuse error: {}", err),
                        APIError::FeeRateTooHigh {ref err, ref feerate} => write!(f, "{} feerate: {}", err, feerate),
-                       APIError::RouteError {ref err} => f.write_str(err),
-                       APIError::ChannelUnavailable {ref err} => f.write_str(err),
-                       APIError::MonitorUpdateFailed => f.write_str("Client indicated a channel monitor update failed"),
+                       APIError::InvalidRoute {ref err} => write!(f, "Invalid route provided: {}", err),
+                       APIError::ChannelUnavailable {ref err} => write!(f, "Channel unavailable: {}", err),
+                       APIError::MonitorUpdateInProgress => f.write_str("Client indicated a channel monitor update is in progress but not yet complete"),
                        APIError::IncompatibleShutdownScript { ref script } => {
                                write!(f, "Provided a scriptpubkey format not accepted by peer: {}", script)
                        },
@@ -78,6 +86,18 @@ impl fmt::Debug for APIError {
        }
 }
 
+impl_writeable_tlv_based_enum_upgradable!(APIError,
+       (0, APIMisuseError) => { (0, err, required), },
+       (2, FeeRateTooHigh) => {
+               (0, err, required),
+               (2, feerate, required),
+       },
+       (4, InvalidRoute) => { (0, err, required), },
+       (6, ChannelUnavailable) => { (0, err, required), },
+       (8, MonitorUpdateInProgress) => {},
+       (10, IncompatibleShutdownScript) => { (0, script, required), },
+);
+
 #[inline]
 pub(crate) fn get_onion_debug_field(error_code: u16) -> (&'static str, usize) {
        match error_code & 0xff {
@@ -119,6 +139,8 @@ pub(crate) fn get_onion_error_description(error_code: u16) -> (&'static str, &'s
                _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"),
                _c if _c == UPDATE|20 => ("Node indicated the outbound channel has been disabled", "channel_disabled"),
                _c if _c == 21 => ("Node indicated the CLTV expiry in the HTLC is too far in the future", "expiry_too_far"),
+               _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"),
+               _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"),
                _ => ("Unknown", ""),
        }
 }