Skip to content

Commit

Permalink
Merge #360: Clean jsonrpc error module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darosior committed Feb 9, 2022
2 parents 61f0174 + 39aae32 commit 843e513
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 156 deletions.
61 changes: 61 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,67 @@ impl From<revault_tx::Error> for CommandError {
}
}

impl CommandError {
pub fn code(&self) -> ErrorCode {
match self {
CommandError::UnknownOutpoint(_) => ErrorCode::RESOURCE_NOT_FOUND_ERROR,
CommandError::InvalidStatus(..) => ErrorCode::INVALID_STATUS_ERROR,
CommandError::InvalidStatusFor(..) => ErrorCode::INVALID_STATUS_ERROR,
CommandError::InvalidParams(_) => ErrorCode::INVALID_PARAMS,
CommandError::Communication(e) => match e {
CommunicationError::Net(_) => ErrorCode::TRANSPORT_ERROR,
CommunicationError::WatchtowerNack(_, _) => ErrorCode::WT_SIG_NACK,
CommunicationError::SignatureStorage => ErrorCode::COORDINATOR_SIG_STORE_ERROR,
CommunicationError::SpendTxStorage => ErrorCode::COORDINATOR_SPEND_STORE_ERROR,
CommunicationError::CosigAlreadySigned => ErrorCode::COSIGNER_ALREADY_SIGN_ERROR,
CommunicationError::CosigInsanePsbt => ErrorCode::COSIGNER_INSANE_ERROR,
},
CommandError::Bitcoind(_) => ErrorCode::BITCOIND_ERROR,
CommandError::Tx(_) => ErrorCode::INTERNAL_ERROR,
CommandError::SpendFeerateTooLow(_, _) => ErrorCode::INVALID_PARAMS,
// TODO: some of these probably need specific error codes
CommandError::SpendTooLarge
| CommandError::SpendUnknownUnVault(_)
| CommandError::UnknownSpend(_)
| CommandError::SpendSpent(_)
| CommandError::SpendNotEnoughSig(_, _)
| CommandError::SpendInvalidSig(_)
| CommandError::MissingCpfpKey => ErrorCode::INVALID_PARAMS,

CommandError::StakeholderOnly | CommandError::ManagerOnly => ErrorCode::INVALID_REQUEST,
CommandError::Race => ErrorCode::INTERNAL_ERROR,
}
}
}

#[allow(non_camel_case_types)]
pub enum ErrorCode {
/// Invalid Params (identical to jsonrpc error code)
INVALID_PARAMS = -32602,
/// Invalid Request (identical to jsonrpc error code)
INVALID_REQUEST = -32600,
/// Internal error (identical to jsonrpc error code)
INTERNAL_ERROR = -32603,
/// An error internal to revault_net, generally a transport error
TRANSPORT_ERROR = 12000,
/// The watchtower refused our signatures
WT_SIG_NACK = 13_000,
/// The Coordinator told us they could not store our signature
COORDINATOR_SIG_STORE_ERROR = 13100,
/// The Coordinator told us they could not store our Spend transaction
COORDINATOR_SPEND_STORE_ERROR = 13101,
/// The Cosigning Server returned null to our request!
COSIGNER_ALREADY_SIGN_ERROR = 13201,
/// The Cosigning Server tried to fool us!
COSIGNER_INSANE_ERROR = 13202,
/// Bitcoind error
BITCOIND_ERROR = 14000,
/// Resource not found
RESOURCE_NOT_FOUND_ERROR = 15000,
/// Vault status was invalid
INVALID_STATUS_ERROR = 15001,
}

macro_rules! stakeholder_only {
($revaultd:ident) => {
if !$revaultd.is_stakeholder() {
Expand Down
16 changes: 12 additions & 4 deletions src/jsonrpc/api/mod.rs → src/jsonrpc/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
//! *valid* JSONRPC2 commands here. All the communication and parsing is done in the
//! `server` mod.

mod error;

use crate::{
commands::{HistoryEventKind, ListSpendStatus},
commands::{CommandError, HistoryEventKind, ListSpendStatus},
revaultd::VaultStatus,
DaemonControl,
};
Expand All @@ -27,10 +25,20 @@ use std::{
},
};

use jsonrpc_core::Error as JsonRpcError;
use jsonrpc_core::{types::error::ErrorCode::ServerError, Error as JsonRpcError};
use jsonrpc_derive::rpc;
use serde_json::json;

impl From<CommandError> for JsonRpcError {
fn from(e: CommandError) -> Self {
JsonRpcError {
code: ServerError(e.code() as i64),
message: e.to_string(),
data: None,
}
}
}

#[derive(Clone)]
pub struct JsonRpcMetaData {
pub shutdown: Arc<AtomicBool>,
Expand Down
152 changes: 0 additions & 152 deletions src/jsonrpc/api/error.rs

This file was deleted.

0 comments on commit 843e513

Please sign in to comment.