Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message #942

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,113 @@ where
Err(err) => bail!("batch {} request failed: {}", name, err),
}
}

macro_rules! define_error_codes {
($($(#[doc = $doc:expr])* $name:ident = $value:expr,)*) => {
/// Bitcoin Core RPC error codes
/// * https://github.com/bitcoin/bitcoin/blob/master/src/rpc/protocol.h
#[derive(Debug, Clone, Copy)]
#[repr(i32)]
pub(crate) enum CoreRPCErrorCode {
$(
$(#[doc = $doc])*
$name = $value,
)*
}

impl CoreRPCErrorCode {
pub(crate) fn from_error_code(code: i32) -> Option<Self> {
Some(match code {
$(
$value => Self::$name,
)*
_ => return None,
})
}
}
}
}

define_error_codes! {
/// RpcInvalidRequest is mapped to HTTP_BAD_REQUEST (400).
/// It is not used for application-layer errors
RpcInvalidRequest = -32600,
/// RpcMethodNotFound is mapped to HTTP_NOT_FOUND (404).
/// It is not be used for application-layer errors.
RpcMethodNotFound = -32601,
RpcInvalidParams = -32602,
/// RpcInternalError is used for genuine errors in bitcoind
RpcInternalError = -32603,
RpcParseError = -32700,

/// std::exception thrown in command handling
RpcMiscError = -1,
/// Unexpected type was passed as parameter
RpcTypeError = -3,
/// Invalid address or key
RpcInvalidAddressOrKey = -5,
/// Ran out of memory during operation
RpcOutOfMemory = -7,
/// Invalid, missing or duplicate parameter
RpcInvalidParameter = -8,
/// Database error
RpcDatabaseError = -20,
/// Error parsing or validating structure in raw format
RpcDeserializationError = -22,
/// General error during transaction or block submission
RpcVerifyError = -25,
/// Transaction or block was rejected by network rules
RpcVerifyRejected = -26,
/// Transaction already in chain
RpcVerifyAlreadyInChain = -27,
/// Client still warming up
RpcInWarmup = -28,
/// RPC method is deprecated
RpcMethodDeprecated = -32,

/// Bitcoin is not connected
RpcClientNotConnected = -9,
/// Still downloading initial blocks
RpcClientInInitialDownload = -10,
/// Node is already added
RpcClientNodeAlreadyAdded = -23,
/// Node has not been added before
RpcClientNodeNotAdded = -24,
/// Node to disconnect not found in connected nodes
RpcClientNodeNotConnected = -29,
/// Invalid IP/Subnet
RpcClientInvalidIpOrSubnet = -30,
/// No valid connection manager instance found
RpcClientP2pDisabled = -31,
/// Max number of outbound or block-relay connections already open
RpcClientNodeCapacityReached = -34,
/// No mempool instance found
RpcClientMempoolDisabled = -33,

/// Unspecified problem with wallet (key not found etc.)
RpcWalletError = -4,
/// Not enough funds in wallet or account
RpcWalletInsufficientFunds = -6,
/// Invalid label name
RpcWalletInvalidLabelName = -11,
/// Keypool ran out, call keypoolrefill first
RpcWalletKeypoolRanOut = -12,
/// Enter the wallet passphrase with walletpassphrase
RpcWalletUnlockNeeded = -13,
/// The wallet passphrase entered was incorrect
RpcWalletPassphraseIncorrect = -14,
/// Command given in wrong wallet encryption state (encrypting an encrypted wallet etc.)
RpcWalletWrongEncState = -15,
/// Failed to encrypt the wallet
RpcWalletEncryptionFailed = -16,
/// Wallet is already unlocked
RpcWalletAlreadyUnlocked = -17,
/// Invalid wallet specified
RpcWalletNotFound = -18,
/// No wallet specified (error when there are multiple wallets loaded)
RpcWalletNotSpecified = -19,
/// This same wallet is already loaded
RpcWalletAlreadyLoaded = -35,
/// There is already a wallet with the same name
RpcWalletAlreadyExisits = -36,
}
18 changes: 16 additions & 2 deletions src/electrum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::str::FromStr;
use crate::{
cache::Cache,
config::{Config, ELECTRS_VERSION},
daemon::{self, extract_bitcoind_error, Daemon},
daemon::{self, extract_bitcoind_error, CoreRPCErrorCode, Daemon},
merkle::Proof,
metrics::{self, Histogram, Metrics},
signals::Signal,
Expand Down Expand Up @@ -624,6 +624,17 @@ impl Params {
}
})
}
fn parse_rpc_error_code(&self, code: i32) -> Option<RpcError> {
match self {
Params::TransactionGet(_) => match CoreRPCErrorCode::from_error_code(code) {
Some(CoreRPCErrorCode::RpcInvalidAddressOrKey) => {
Some(RpcError::BadRequest(anyhow!("Transaction not found")))
}
_ => None,
},
_ => None,
}
}
}

struct Call {
Expand Down Expand Up @@ -653,7 +664,10 @@ impl Call {
.downcast_ref::<bitcoincore_rpc::Error>()
.and_then(extract_bitcoind_error)
{
Some(e) => error_msg(&self.id, RpcError::DaemonError(e.clone())),
Some(e) => match self.params.parse_rpc_error_code(e.code) {
Some(err) => error_msg(&self.id, err),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this is sending the underlying error message to the caller. And while it's not recommended to connect untrusted devices to electrs some people may do that and it'd be better to just log the message and send back "internal server error" to avoid leaking information.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC this is sending the underlying error message to the caller.

It is not. I added self.params.parse_rpc_error_code to allow certain errors to be intercepted and replaced but the underlying error message is not sent to the caller

None => error_msg(&self.id, RpcError::DaemonError(e.clone())),
},
None => error_msg(&self.id, RpcError::BadRequest(err)),
}
}
Expand Down
Loading