This repository was archived by the owner on Feb 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy patherror.rs
More file actions
207 lines (197 loc) · 7.32 KB
/
error.rs
File metadata and controls
207 lines (197 loc) · 7.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use bdk::esplora_client;
use lightning::ln::peer_handler::PeerHandleError;
use thiserror::Error;
use wasm_bindgen::JsValue;
#[derive(Error, Debug)]
#[allow(dead_code)]
// copied from LDK lite
/// An error that possibly needs to be handled by the user.
pub enum MutinyError {
/// Returned when trying to start Mutiny while it is already running.
#[error("Mutiny is already running.")]
AlreadyRunning,
/// Returned when trying to stop Mutiny while it is not running.
#[error("Mutiny is not running.")]
NotRunning,
/// The funding transaction could not be created.
#[error("Funding transaction could not be created.")]
FundingTxCreationFailed,
/// A network connection has been closed.
#[error("Network connection closed.")]
ConnectionFailed,
/// Payment of the given invoice has already been initiated.
#[error("An invoice must not get payed twice.")]
NonUniquePaymentHash,
/// The given invoice is invalid.
#[error("The given invoice is invalid.")]
InvoiceInvalid,
/// Invoice creation failed.
#[error("Failed to create invoice.")]
InvoiceCreationFailed,
/// No route for the given target could be found.
#[error("Failed to find route.")]
RoutingFailed,
/// A given peer info could not be parsed.
#[error("Failed to parse the given peer information.")]
PeerInfoParseFailed,
/// A channel could not be opened.
#[error("Failed to create channel.")]
ChannelCreationFailed,
/// A channel could not be closed.
#[error("Failed to close channel.")]
ChannelClosingFailed,
/// Persistence failed.
#[error("Failed to persist data.")]
PersistenceFailed {
#[from]
source: MutinyStorageError,
},
#[error("Failed to read data from storage.")]
ReadError { source: MutinyStorageError },
/// A wallet operation failed.
#[error("Failed to conduct wallet operation.")]
WalletOperationFailed,
/// A signing operation failed.
#[error("Failed to sign given transaction.")]
WalletSigningFailed,
/// A chain access operation failed.
#[error("Failed to conduct chain access operation.")]
ChainAccessFailed,
#[error(transparent)]
Other(#[from] anyhow::Error),
}
#[derive(Error, Debug)]
pub enum MutinyStorageError {
#[error("Failed to use browser storage")]
StorageError {
#[from]
source: gloo_storage::errors::StorageError,
},
#[error("Failed to serialize or deserialize")]
SerdeError {
#[from]
source: serde_json::Error,
},
#[error(transparent)]
Other(#[from] anyhow::Error),
}
impl MutinyError {
pub fn read_err(e: MutinyStorageError) -> Self {
MutinyError::ReadError { source: e }
}
}
impl From<bdk::Error> for MutinyError {
fn from(e: bdk::Error) -> Self {
match e {
bdk::Error::Signer(_) => Self::WalletSigningFailed,
_ => Self::WalletOperationFailed,
}
}
}
// TODO add more granular errors for esplora failures
impl From<esplora_client::Error> for MutinyError {
fn from(_e: esplora_client::Error) -> Self {
// This is most likely a chain access failure
Self::ChainAccessFailed
}
}
impl From<PeerHandleError> for MutinyError {
fn from(_e: PeerHandleError) -> Self {
// TODO handle the case where `no_connection_possible`
Self::ConnectionFailed
}
}
impl From<MutinyStorageError> for bdk::Error {
fn from(e: MutinyStorageError) -> Self {
match e {
MutinyStorageError::StorageError { source } => {
bdk::Error::Generic(format!("Storage error: {source}"))
}
MutinyStorageError::SerdeError { source } => {
bdk::Error::Generic(format!("Serde error: {source}"))
}
_ => bdk::Error::Generic("Unexpected Mutiny storage Error".to_string()),
}
}
}
#[derive(Error, Debug)]
pub enum MutinyJsError {
/// Returned when trying to start Mutiny while it is already running.
#[error("Mutiny is already running.")]
AlreadyRunning,
/// Returned when trying to stop Mutiny while it is not running.
#[error("Mutiny is not running.")]
NotRunning,
/// The funding transaction could not be created.
#[error("Funding transaction could not be created.")]
FundingTxCreationFailed,
/// A network connection has been closed.
#[error("Network connection closed.")]
ConnectionFailed,
/// Payment of the given invoice has already been initiated.
#[error("An invoice must not get payed twice.")]
NonUniquePaymentHash,
/// The given invoice is invalid.
#[error("The given invoice is invalid.")]
InvoiceInvalid,
/// Invoice creation failed.
#[error("Failed to create invoice.")]
InvoiceCreationFailed,
/// No route for the given target could be found.
#[error("Failed to find route.")]
RoutingFailed,
/// A given peer info could not be parsed.
#[error("Failed to parse the given peer information.")]
PeerInfoParseFailed,
/// A channel could not be opened.
#[error("Failed to create channel.")]
ChannelCreationFailed,
/// A channel could not be closed.
#[error("Failed to close channel.")]
ChannelClosingFailed,
/// Persistence failed.
#[error("Failed to persist data.")]
PersistenceFailed,
#[error("Failed to read data from storage.")]
ReadError,
/// A wallet operation failed.
#[error("Failed to conduct wallet operation.")]
WalletOperationFailed,
/// A signing operation failed.
#[error("Failed to sign given transaction.")]
WalletSigningFailed,
/// A chain access operation failed.
#[error("Failed to conduct chain access operation.")]
ChainAccessFailed,
/// Unknown error.
#[error("Unknown Error")]
UnknownError,
}
impl From<MutinyError> for MutinyJsError {
fn from(e: MutinyError) -> Self {
match e {
MutinyError::AlreadyRunning => MutinyJsError::AlreadyRunning,
MutinyError::NotRunning => MutinyJsError::NotRunning,
MutinyError::FundingTxCreationFailed => MutinyJsError::FundingTxCreationFailed,
MutinyError::ConnectionFailed => MutinyJsError::ConnectionFailed,
MutinyError::NonUniquePaymentHash => MutinyJsError::NonUniquePaymentHash,
MutinyError::InvoiceInvalid => MutinyJsError::InvoiceInvalid,
MutinyError::InvoiceCreationFailed => MutinyJsError::InvoiceCreationFailed,
MutinyError::RoutingFailed => MutinyJsError::RoutingFailed,
MutinyError::PeerInfoParseFailed => MutinyJsError::PeerInfoParseFailed,
MutinyError::ChannelCreationFailed => MutinyJsError::ChannelCreationFailed,
MutinyError::ChannelClosingFailed => MutinyJsError::ChannelClosingFailed,
MutinyError::PersistenceFailed { source: _ } => MutinyJsError::PersistenceFailed,
MutinyError::ReadError { source: _ } => MutinyJsError::ReadError,
MutinyError::WalletOperationFailed => MutinyJsError::WalletOperationFailed,
MutinyError::WalletSigningFailed => MutinyJsError::WalletSigningFailed,
MutinyError::ChainAccessFailed => MutinyJsError::ChainAccessFailed,
MutinyError::Other(_) => MutinyJsError::UnknownError,
}
}
}
impl From<MutinyJsError> for JsValue {
fn from(e: MutinyJsError) -> Self {
JsValue::from(e.to_string())
}
}