ldk_node/
error.rs

1// This file is Copyright its original authors, visible in version control history.
2//
3// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
6// accordance with one or both of these licenses.
7
8use bdk_chain::bitcoin::psbt::ExtractTxError as BdkExtractTxError;
9use bdk_chain::local_chain::CannotConnectError as BdkChainConnectionError;
10use bdk_chain::tx_graph::CalculateFeeError as BdkChainCalculateFeeError;
11use bdk_wallet::error::CreateTxError as BdkCreateTxError;
12use bdk_wallet::signer::SignerError as BdkSignerError;
13
14use std::fmt;
15
16#[derive(Copy, Clone, Debug, PartialEq, Eq)]
17/// An error that possibly needs to be handled by the user.
18pub enum Error {
19	/// Returned when trying to start [`crate::Node`] while it is already running.
20	AlreadyRunning,
21	/// Returned when trying to stop [`crate::Node`] while it is not running.
22	NotRunning,
23	/// An on-chain transaction could not be created.
24	OnchainTxCreationFailed,
25	/// A network connection has been closed.
26	ConnectionFailed,
27	/// Invoice creation failed.
28	InvoiceCreationFailed,
29	/// Invoice request creation failed.
30	InvoiceRequestCreationFailed,
31	/// Offer creation failed.
32	OfferCreationFailed,
33	/// Refund creation failed.
34	RefundCreationFailed,
35	/// Sending a payment has failed.
36	PaymentSendingFailed,
37	/// Sending of spontaneous payment with custom TLVs failed.
38	InvalidCustomTlvs,
39	/// Sending a payment probe has failed.
40	ProbeSendingFailed,
41	/// A channel could not be opened.
42	ChannelCreationFailed,
43	/// A channel could not be closed.
44	ChannelClosingFailed,
45	/// A channel configuration could not be updated.
46	ChannelConfigUpdateFailed,
47	/// Persistence failed.
48	PersistenceFailed,
49	/// A fee rate estimation update failed.
50	FeerateEstimationUpdateFailed,
51	/// A fee rate estimation update timed out.
52	FeerateEstimationUpdateTimeout,
53	/// A wallet operation failed.
54	WalletOperationFailed,
55	/// A wallet operation timed out.
56	WalletOperationTimeout,
57	/// A signing operation for transaction failed.
58	OnchainTxSigningFailed,
59	/// A transaction sync operation failed.
60	TxSyncFailed,
61	/// A transaction sync operation timed out.
62	TxSyncTimeout,
63	/// A gossip updating operation failed.
64	GossipUpdateFailed,
65	/// A gossip updating operation timed out.
66	GossipUpdateTimeout,
67	/// A liquidity request operation failed.
68	LiquidityRequestFailed,
69	/// Parsing a URI parameter has failed.
70	UriParameterParsingFailed,
71	/// The given address is invalid.
72	InvalidAddress,
73	/// The given network address is invalid.
74	InvalidSocketAddress,
75	/// The given public key is invalid.
76	InvalidPublicKey,
77	/// The given secret key is invalid.
78	InvalidSecretKey,
79	/// The given offer id is invalid.
80	InvalidOfferId,
81	/// The given node id is invalid.
82	InvalidNodeId,
83	/// The given payment id is invalid.
84	InvalidPaymentId,
85	/// The given payment hash is invalid.
86	InvalidPaymentHash,
87	/// The given payment pre-image is invalid.
88	InvalidPaymentPreimage,
89	/// The given payment secret is invalid.
90	InvalidPaymentSecret,
91	/// The given amount is invalid.
92	InvalidAmount,
93	/// The given invoice is invalid.
94	InvalidInvoice,
95	/// The given offer is invalid.
96	InvalidOffer,
97	/// The given refund is invalid.
98	InvalidRefund,
99	/// The given channel ID is invalid.
100	InvalidChannelId,
101	/// The given network is invalid.
102	InvalidNetwork,
103	/// The given URI is invalid.
104	InvalidUri,
105	/// The given quantity is invalid.
106	InvalidQuantity,
107	/// The given node alias is invalid.
108	InvalidNodeAlias,
109	/// The given date time is invalid.
110	InvalidDateTime,
111	/// The given fee rate is invalid.
112	InvalidFeeRate,
113	/// A payment with the given hash has already been initiated.
114	DuplicatePayment,
115	/// The provided offer was denonminated in an unsupported currency.
116	UnsupportedCurrency,
117	/// The available funds are insufficient to complete the given operation.
118	InsufficientFunds,
119	/// The given operation failed due to the required liquidity source being unavailable.
120	LiquiditySourceUnavailable,
121	/// The given operation failed due to the LSP's required opening fee being too high.
122	LiquidityFeeTooHigh,
123}
124
125impl fmt::Display for Error {
126	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
127		match *self {
128			Self::AlreadyRunning => write!(f, "Node is already running."),
129			Self::NotRunning => write!(f, "Node is not running."),
130			Self::OnchainTxCreationFailed => {
131				write!(f, "On-chain transaction could not be created.")
132			},
133			Self::ConnectionFailed => write!(f, "Network connection closed."),
134			Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
135			Self::InvoiceRequestCreationFailed => write!(f, "Failed to create invoice request."),
136			Self::OfferCreationFailed => write!(f, "Failed to create offer."),
137			Self::RefundCreationFailed => write!(f, "Failed to create refund."),
138			Self::PaymentSendingFailed => write!(f, "Failed to send the given payment."),
139			Self::InvalidCustomTlvs => write!(f, "Failed to construct payment with custom TLVs."),
140			Self::ProbeSendingFailed => write!(f, "Failed to send the given payment probe."),
141			Self::ChannelCreationFailed => write!(f, "Failed to create channel."),
142			Self::ChannelClosingFailed => write!(f, "Failed to close channel."),
143			Self::ChannelConfigUpdateFailed => write!(f, "Failed to update channel config."),
144			Self::PersistenceFailed => write!(f, "Failed to persist data."),
145			Self::FeerateEstimationUpdateFailed => {
146				write!(f, "Failed to update fee rate estimates.")
147			},
148			Self::FeerateEstimationUpdateTimeout => {
149				write!(f, "Updating fee rate estimates timed out.")
150			},
151			Self::WalletOperationFailed => write!(f, "Failed to conduct wallet operation."),
152			Self::WalletOperationTimeout => write!(f, "A wallet operation timed out."),
153			Self::OnchainTxSigningFailed => write!(f, "Failed to sign given transaction."),
154			Self::TxSyncFailed => write!(f, "Failed to sync transactions."),
155			Self::TxSyncTimeout => write!(f, "Syncing transactions timed out."),
156			Self::GossipUpdateFailed => write!(f, "Failed to update gossip data."),
157			Self::GossipUpdateTimeout => write!(f, "Updating gossip data timed out."),
158			Self::LiquidityRequestFailed => write!(f, "Failed to request inbound liquidity."),
159			Self::UriParameterParsingFailed => write!(f, "Failed to parse a URI parameter."),
160			Self::InvalidAddress => write!(f, "The given address is invalid."),
161			Self::InvalidSocketAddress => write!(f, "The given network address is invalid."),
162			Self::InvalidPublicKey => write!(f, "The given public key is invalid."),
163			Self::InvalidSecretKey => write!(f, "The given secret key is invalid."),
164			Self::InvalidOfferId => write!(f, "The given offer id is invalid."),
165			Self::InvalidNodeId => write!(f, "The given node id is invalid."),
166			Self::InvalidPaymentId => write!(f, "The given payment id is invalid."),
167			Self::InvalidPaymentHash => write!(f, "The given payment hash is invalid."),
168			Self::InvalidPaymentPreimage => write!(f, "The given payment preimage is invalid."),
169			Self::InvalidPaymentSecret => write!(f, "The given payment secret is invalid."),
170			Self::InvalidAmount => write!(f, "The given amount is invalid."),
171			Self::InvalidInvoice => write!(f, "The given invoice is invalid."),
172			Self::InvalidOffer => write!(f, "The given offer is invalid."),
173			Self::InvalidRefund => write!(f, "The given refund is invalid."),
174			Self::InvalidChannelId => write!(f, "The given channel ID is invalid."),
175			Self::InvalidNetwork => write!(f, "The given network is invalid."),
176			Self::InvalidUri => write!(f, "The given URI is invalid."),
177			Self::InvalidQuantity => write!(f, "The given quantity is invalid."),
178			Self::InvalidNodeAlias => write!(f, "The given node alias is invalid."),
179			Self::InvalidDateTime => write!(f, "The given date time is invalid."),
180			Self::InvalidFeeRate => write!(f, "The given fee rate is invalid."),
181			Self::DuplicatePayment => {
182				write!(f, "A payment with the given hash has already been initiated.")
183			},
184			Self::InsufficientFunds => {
185				write!(f, "The available funds are insufficient to complete the given operation.")
186			},
187			Self::UnsupportedCurrency => {
188				write!(f, "The provided offer was denonminated in an unsupported currency.")
189			},
190			Self::LiquiditySourceUnavailable => {
191				write!(f, "The given operation failed due to the required liquidity source being unavailable.")
192			},
193			Self::LiquidityFeeTooHigh => {
194				write!(f, "The given operation failed due to the LSP's required opening fee being too high.")
195			},
196		}
197	}
198}
199
200impl std::error::Error for Error {}
201
202impl From<BdkSignerError> for Error {
203	fn from(_: BdkSignerError) -> Self {
204		Self::OnchainTxSigningFailed
205	}
206}
207
208impl From<BdkCreateTxError> for Error {
209	fn from(e: BdkCreateTxError) -> Self {
210		match e {
211			BdkCreateTxError::CoinSelection(_) => Self::InsufficientFunds,
212			_ => Self::OnchainTxCreationFailed,
213		}
214	}
215}
216
217impl From<BdkExtractTxError> for Error {
218	fn from(_: BdkExtractTxError) -> Self {
219		Self::OnchainTxCreationFailed
220	}
221}
222
223impl From<BdkChainConnectionError> for Error {
224	fn from(_: BdkChainConnectionError) -> Self {
225		Self::WalletOperationFailed
226	}
227}
228
229impl From<BdkChainCalculateFeeError> for Error {
230	fn from(_: BdkChainCalculateFeeError) -> Self {
231		Self::WalletOperationFailed
232	}
233}
234
235impl From<lightning_transaction_sync::TxSyncError> for Error {
236	fn from(_e: lightning_transaction_sync::TxSyncError) -> Self {
237		Self::TxSyncFailed
238	}
239}