From fbaa2c332e879e508d33b63b0f39198f3da74a67 Mon Sep 17 00:00:00 2001 From: Novo Date: Tue, 17 Oct 2023 09:15:35 +0100 Subject: [PATCH] Hndl RPC_INVALID_ADDRESS_OR_KEY err for transaction.get --- src/electrum.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/electrum.rs b/src/electrum.rs index fed999454..47015a20f 100644 --- a/src/electrum.rs +++ b/src/electrum.rs @@ -5,6 +5,7 @@ use bitcoin::{ BlockHash, Txid, }; use crossbeam_channel::Receiver; +use num_traits::FromPrimitive; use rayon::prelude::*; use serde_derive::Deserialize; use serde_json::{self, json, Value}; @@ -17,7 +18,7 @@ use std::str::FromStr; use crate::{ cache::Cache, config::{Config, ELECTRS_VERSION}, - daemon::{self, extract_bitcoind_error, Daemon}, + daemon::{self, extract_bitcoind_error, Daemon, RPCErrorCode}, merkle::Proof, metrics::{self, Histogram, Metrics}, signals::Signal, @@ -624,6 +625,17 @@ impl Params { } }) } + fn parse_rpc_error_code(&self, code: i32) -> Option { + match self { + Params::TransactionGet(_) => match RPCErrorCode::from_i32(code) { + Some(RPCErrorCode::RpcInvalidAddressOrKey) => { + Some(RpcError::BadRequest(anyhow!("Transaction not found"))) + } + _ => None, + }, + _ => None, + } + } } struct Call { @@ -653,7 +665,10 @@ impl Call { .downcast_ref::() .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), + None => error_msg(&self.id, RpcError::DaemonError(e.clone())), + }, None => error_msg(&self.id, RpcError::BadRequest(err)), } }