lightning/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
12use crate::ln::script::ShutdownScript;
13
14#[allow(unused_imports)]
15use crate::prelude::*;
16
17use core::fmt;
18
19/// Indicates an error on the client's part (usually some variant of attempting to use too-low or
20/// too-high values)
21#[derive(Clone, PartialEq, Eq)]
22pub enum APIError {
23	/// Indicates the API was wholly misused (see err for more). Cases where these can be returned
24	/// are documented, but generally indicates some precondition of a function was violated.
25	APIMisuseError {
26		/// A human-readable error message
27		err: String,
28	},
29	/// Due to a high feerate, we were unable to complete the request.
30	/// For example, this may be returned if the feerate implies we cannot open a channel at the
31	/// requested value, but opening a larger channel would succeed.
32	FeeRateTooHigh {
33		/// A human-readable error message
34		err: String,
35		/// The feerate which was too high.
36		feerate: u32,
37	},
38	/// A malformed Route was provided (eg overflowed value, node id mismatch, overly-looped route,
39	/// too-many-hops, etc).
40	InvalidRoute {
41		/// A human-readable error message
42		err: String,
43	},
44	/// We were unable to complete the request as the Channel required to do so is unable to
45	/// complete the request (or was not found). This can take many forms, including disconnected
46	/// peer, channel at capacity, channel shutting down, etc.
47	ChannelUnavailable {
48		/// A human-readable error message
49		err: String,
50	},
51	/// An attempt to call [`chain::Watch::watch_channel`]/[`chain::Watch::update_channel`]
52	/// returned a [`ChannelMonitorUpdateStatus::InProgress`] indicating the persistence of a
53	/// monitor update is awaiting async resolution. Once it resolves the attempted action should
54	/// complete automatically.
55	///
56	/// [`chain::Watch::watch_channel`]: crate::chain::Watch::watch_channel
57	/// [`chain::Watch::update_channel`]: crate::chain::Watch::update_channel
58	/// [`ChannelMonitorUpdateStatus::InProgress`]: crate::chain::ChannelMonitorUpdateStatus::InProgress
59	MonitorUpdateInProgress,
60	/// [`SignerProvider::get_shutdown_scriptpubkey`] returned a shutdown scriptpubkey incompatible
61	/// with the channel counterparty as negotiated in [`InitFeatures`].
62	///
63	/// Using a SegWit v0 script should resolve this issue. If you cannot, you won't be able to open
64	/// a channel or cooperatively close one with this peer (and will have to force-close instead).
65	///
66	/// [`SignerProvider::get_shutdown_scriptpubkey`]: crate::sign::SignerProvider::get_shutdown_scriptpubkey
67	/// [`InitFeatures`]: crate::types::features::InitFeatures
68	IncompatibleShutdownScript {
69		/// The incompatible shutdown script.
70		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);