Skip to content

Commit 843e513

Browse files
committed
Merge #360: Clean jsonrpc error module
39aae32 Clean jsonrpc error module (edouard) Pull request description: Since jsonrpc server uses the daemon commands interface, the errors are handled by the unique enum CommandError and all the helpers in the jsonrpc api error module are deprecated. The ErrorCode enum is moved to the command module in order to be exposed for the consumers importing the lib without the jsonrcp server feature. ACKs for top commit: darosior: utACK 39aae32 Tree-SHA512: 4cef770fdb556eeeb6d8eded955f19ddfdb452321150020d91dc80768240765e818dce5b70d44eaee3223984c31282a39aa3a77109e3f85e413631314c0fef94
2 parents 61f0174 + 39aae32 commit 843e513

File tree

3 files changed

+73
-156
lines changed

3 files changed

+73
-156
lines changed

src/commands/mod.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,67 @@ impl From<revault_tx::Error> for CommandError {
170170
}
171171
}
172172

173+
impl CommandError {
174+
pub fn code(&self) -> ErrorCode {
175+
match self {
176+
CommandError::UnknownOutpoint(_) => ErrorCode::RESOURCE_NOT_FOUND_ERROR,
177+
CommandError::InvalidStatus(..) => ErrorCode::INVALID_STATUS_ERROR,
178+
CommandError::InvalidStatusFor(..) => ErrorCode::INVALID_STATUS_ERROR,
179+
CommandError::InvalidParams(_) => ErrorCode::INVALID_PARAMS,
180+
CommandError::Communication(e) => match e {
181+
CommunicationError::Net(_) => ErrorCode::TRANSPORT_ERROR,
182+
CommunicationError::WatchtowerNack(_, _) => ErrorCode::WT_SIG_NACK,
183+
CommunicationError::SignatureStorage => ErrorCode::COORDINATOR_SIG_STORE_ERROR,
184+
CommunicationError::SpendTxStorage => ErrorCode::COORDINATOR_SPEND_STORE_ERROR,
185+
CommunicationError::CosigAlreadySigned => ErrorCode::COSIGNER_ALREADY_SIGN_ERROR,
186+
CommunicationError::CosigInsanePsbt => ErrorCode::COSIGNER_INSANE_ERROR,
187+
},
188+
CommandError::Bitcoind(_) => ErrorCode::BITCOIND_ERROR,
189+
CommandError::Tx(_) => ErrorCode::INTERNAL_ERROR,
190+
CommandError::SpendFeerateTooLow(_, _) => ErrorCode::INVALID_PARAMS,
191+
// TODO: some of these probably need specific error codes
192+
CommandError::SpendTooLarge
193+
| CommandError::SpendUnknownUnVault(_)
194+
| CommandError::UnknownSpend(_)
195+
| CommandError::SpendSpent(_)
196+
| CommandError::SpendNotEnoughSig(_, _)
197+
| CommandError::SpendInvalidSig(_)
198+
| CommandError::MissingCpfpKey => ErrorCode::INVALID_PARAMS,
199+
200+
CommandError::StakeholderOnly | CommandError::ManagerOnly => ErrorCode::INVALID_REQUEST,
201+
CommandError::Race => ErrorCode::INTERNAL_ERROR,
202+
}
203+
}
204+
}
205+
206+
#[allow(non_camel_case_types)]
207+
pub enum ErrorCode {
208+
/// Invalid Params (identical to jsonrpc error code)
209+
INVALID_PARAMS = -32602,
210+
/// Invalid Request (identical to jsonrpc error code)
211+
INVALID_REQUEST = -32600,
212+
/// Internal error (identical to jsonrpc error code)
213+
INTERNAL_ERROR = -32603,
214+
/// An error internal to revault_net, generally a transport error
215+
TRANSPORT_ERROR = 12000,
216+
/// The watchtower refused our signatures
217+
WT_SIG_NACK = 13_000,
218+
/// The Coordinator told us they could not store our signature
219+
COORDINATOR_SIG_STORE_ERROR = 13100,
220+
/// The Coordinator told us they could not store our Spend transaction
221+
COORDINATOR_SPEND_STORE_ERROR = 13101,
222+
/// The Cosigning Server returned null to our request!
223+
COSIGNER_ALREADY_SIGN_ERROR = 13201,
224+
/// The Cosigning Server tried to fool us!
225+
COSIGNER_INSANE_ERROR = 13202,
226+
/// Bitcoind error
227+
BITCOIND_ERROR = 14000,
228+
/// Resource not found
229+
RESOURCE_NOT_FOUND_ERROR = 15000,
230+
/// Vault status was invalid
231+
INVALID_STATUS_ERROR = 15001,
232+
}
233+
173234
macro_rules! stakeholder_only {
174235
($revaultd:ident) => {
175236
if !$revaultd.is_stakeholder() {

src/jsonrpc/api/mod.rs renamed to src/jsonrpc/api.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22
//! *valid* JSONRPC2 commands here. All the communication and parsing is done in the
33
//! `server` mod.
44
5-
mod error;
6-
75
use crate::{
8-
commands::{HistoryEventKind, ListSpendStatus},
6+
commands::{CommandError, HistoryEventKind, ListSpendStatus},
97
revaultd::VaultStatus,
108
DaemonControl,
119
};
@@ -27,10 +25,20 @@ use std::{
2725
},
2826
};
2927

30-
use jsonrpc_core::Error as JsonRpcError;
28+
use jsonrpc_core::{types::error::ErrorCode::ServerError, Error as JsonRpcError};
3129
use jsonrpc_derive::rpc;
3230
use serde_json::json;
3331

32+
impl From<CommandError> for JsonRpcError {
33+
fn from(e: CommandError) -> Self {
34+
JsonRpcError {
35+
code: ServerError(e.code() as i64),
36+
message: e.to_string(),
37+
data: None,
38+
}
39+
}
40+
}
41+
3442
#[derive(Clone)]
3543
pub struct JsonRpcMetaData {
3644
pub shutdown: Arc<AtomicBool>,

src/jsonrpc/api/error.rs

Lines changed: 0 additions & 152 deletions
This file was deleted.

0 commit comments

Comments
 (0)