Skip to content

Commit

Permalink
Improve RPC errors Display
Browse files Browse the repository at this point in the history
Example with new formatting:

  sendrawtransaction RPC error -22: TX decode failed. Make sure the tx has at least one input.

Instead of:

  sendrawtransaction RPC error -22: {"code":-22,"message":"TX decode failed. Make sure the tx has at least one input."}
  • Loading branch information
shesek committed Dec 3, 2024
1 parent d4a1360 commit 1250b19
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ fn parse_jsonrpc_reply(mut reply: Value, method: &str, expected_id: u64) -> Resu
if let Some(err) = reply_obj.get_mut("error") {
if !err.is_null() {
if let Some(code) = parse_error_code(&err) {
let msg = err["message"]
.as_str()
.map_or_else(|| err.to_string(), |s| s.to_string());
match code {
// RPC_IN_WARMUP -> retry by later reconnection
-28 => bail!(ErrorKind::Connection(err.to_string())),
code => bail!(ErrorKind::RpcError(code, err.take(), method.to_string())),
code => bail!(ErrorKind::RpcError(code, msg, method.to_string())),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error_chain! {
display("Connection error: {}", msg)
}

RpcError(code: i64, error: serde_json::Value, method: String) {
RpcError(code: i64, error: String, method: String) {
description("RPC error")
display("{} RPC error {}: {}", method, code, error)
}
Expand Down

0 comments on commit 1250b19

Please sign in to comment.