From a66615f125a0574eef83a49378989f71598c9159 Mon Sep 17 00:00:00 2001 From: Nadav Ivgi Date: Tue, 3 Dec 2024 04:35:13 +0200 Subject: [PATCH] Improve RPC errors Display 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."} --- src/daemon.rs | 5 ++++- src/errors.rs | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/daemon.rs b/src/daemon.rs index 680890792..3aa1f0f3c 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -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())), } } } diff --git a/src/errors.rs b/src/errors.rs index c708d7dda..2c33f9fac 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -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) }