From cb108738f8d795d431b910146685386bb8d41094 Mon Sep 17 00:00:00 2001 From: Vihiga Tyonum Date: Sat, 8 Mar 2025 04:46:22 +0100 Subject: [PATCH] refactor: fix error handling -add more error types to error enum - update the handlers file to replace Generic error arm [Ticket: X] --- ci/test_features.sh | 3 --- src/commands.rs | 4 ++-- src/error.rs | 8 ++++++- src/handlers.rs | 52 +++++++++++++++++---------------------------- src/utils.rs | 9 +++++--- 5 files changed, 34 insertions(+), 42 deletions(-) diff --git a/ci/test_features.sh b/ci/test_features.sh index 7407818..ecef034 100755 --- a/ci/test_features.sh +++ b/ci/test_features.sh @@ -20,13 +20,10 @@ feature_combinations=( "sqlite esplora" "sqlite verify" "sqlite compiler" - "rpc esplora" - "rpc electrum" "verify esplora compiler" "verify esplora repl" "verify compiler repl" "verify esplora compiler repl" - "rpc esplora compiler" "rpc compiler electrum" "sqlite rpc compiler" ) diff --git a/src/commands.rs b/src/commands.rs index 9d22a79..2046870 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -195,8 +195,8 @@ pub struct WalletOpts { #[cfg(feature = "rpc")] /// Sets an optional cookie authentication. - #[arg(name = "COOKIE", long)] - pub cookie: Option + #[clap(name = "COOKIE", long = "cookie")] + pub cookie: Option, } /// Options to configure a SOCKS5 proxy for a blockchain client connection. diff --git a/src/error.rs b/src/error.rs index 2f18854..ebacc8d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -4,7 +4,6 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum BDKCliError { - #[error("BIP39 error: {0}")] BIP39Error(#[from] bdk_wallet::bip39::Error), @@ -39,6 +38,9 @@ pub enum BDKCliError { #[error("Key error: {0}")] KeyError(#[from] bdk_wallet::keys::KeyError), + #[error("LocalChain error: {0}")] + LocalChainError(#[from] bdk_wallet::chain::local_chain::ApplyHeaderError), + #[error("Miniscript error: {0}")] MiniscriptError(#[from] bdk_wallet::miniscript::Error), @@ -79,4 +81,8 @@ pub enum BDKCliError { #[error("Consensus decoding error: {0}")] Hex(#[from] HexToBytesError), + + #[cfg(feature = "rpc")] + #[error("RPC error: {0}")] + BitcoinCoreRpcError(#[from] bdk_bitcoind_rpc::bitcoincore_rpc::Error), } diff --git a/src/handlers.rs b/src/handlers.rs index 0e58cbd..13c3fe1 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -58,7 +58,7 @@ use {crate::utils::BlockchainClient::Esplora, bdk_esplora::EsploraAsyncExt}; #[cfg(feature = "rpc")] use { crate::utils::BlockchainClient::RpcClient, - bdk_bitcoind_rpc::{Emitter, bitcoincore_rpc::RpcApi}, + bdk_bitcoind_rpc::{bitcoincore_rpc::RpcApi, Emitter}, bdk_wallet::chain::{BlockId, CheckPoint}, }; @@ -361,9 +361,7 @@ pub(crate) async fn handle_online_wallet_subcommand( client .populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx)); - let update = client - .full_scan(request, stop_gap, batch_size, false) - .map_err(|e| Error::Generic(e.to_string()))?; + let update = client.full_scan(request, stop_gap, batch_size, false)?; wallet.apply_update(update)?; } #[cfg(feature = "esplora")] @@ -374,7 +372,7 @@ pub(crate) async fn handle_online_wallet_subcommand( let update = client .full_scan(request, stop_gap, parallel_requests) .await - .map_err(|e| Error::Generic(e.to_string()))?; + .map_err(|e| *e)?; wallet.apply_update(update)?; } @@ -389,20 +387,15 @@ pub(crate) async fn handle_online_wallet_subcommand( let mut emitter = Emitter::new(&client, genesis_cp.clone(), genesis_cp.height()); - while let Some(block_event) = emitter - .next_block() - .map_err(|e| Error::Generic(e.to_string()))? - { - wallet - .apply_block_connected_to( - &block_event.block, - block_event.block_height(), - block_event.connected_to(), - ) - .map_err(|e| Error::Generic(e.to_string()))?; + while let Some(block_event) = emitter.next_block()? { + wallet.apply_block_connected_to( + &block_event.block, + block_event.block_height(), + block_event.connected_to(), + )?; } - let mempool_txs = emitter.mempool().unwrap(); + let mempool_txs = emitter.mempool()?; wallet.apply_unconfirmed_txs(mempool_txs); } } @@ -423,9 +416,7 @@ pub(crate) async fn handle_online_wallet_subcommand( client .populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx)); - let update = client - .sync(request, batch_size, false) - .map_err(|e| Error::Generic(e.to_string()))?; + let update = client.sync(request, batch_size, false)?; wallet.apply_update(update)?; } #[cfg(feature = "esplora")] @@ -436,7 +427,7 @@ pub(crate) async fn handle_online_wallet_subcommand( let update = client .sync(request, parallel_requests) .await - .map_err(|e| Error::Generic(e.to_string()))?; + .map_err(|e| *e)?; wallet.apply_update(update)?; } #[cfg(feature = "rpc")] @@ -444,20 +435,15 @@ pub(crate) async fn handle_online_wallet_subcommand( let wallet_cp = wallet.latest_checkpoint(); let mut emitter = Emitter::new(&client, wallet_cp.clone(), wallet_cp.height()); - while let Some(block_event) = emitter - .next_block() - .map_err(|e| Error::Generic(e.to_string()))? - { - wallet - .apply_block_connected_to( - &block_event.block, - block_event.block_height(), - block_event.connected_to(), - ) - .map_err(|e| Error::Generic(e.to_string()))?; + while let Some(block_event) = emitter.next_block()? { + wallet.apply_block_connected_to( + &block_event.block, + block_event.block_height(), + block_event.connected_to(), + )?; } - let mempool_txs = emitter.mempool().unwrap(); + let mempool_txs = emitter.mempool()?; wallet.apply_unconfirmed_txs(mempool_txs); } } diff --git a/src/utils.rs b/src/utils.rs index cb6ae47..91ccec6 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -192,7 +192,10 @@ pub(crate) fn new_persisted_wallet( network: Network, persister: &mut P, wallet_opts: &WalletOpts, -) -> Result, Error> { +) -> Result, Error> +where + P::Error: std::fmt::Display, +{ let ext_descriptor = wallet_opts.ext_descriptor.clone(); let int_descriptor = wallet_opts.int_descriptor.clone(); @@ -221,14 +224,14 @@ pub(crate) fn new_persisted_wallet( let wallet = Wallet::create(ext_descriptor, int_descriptor) .network(network) .create_wallet(persister) - .map_err(|_| Error::Generic("Can't create wallet.".to_string()))?; + .map_err(|e| Error::Generic(e.to_string()))?; Ok(wallet) } (Some(ext_descriptor), None) => { let wallet = Wallet::create_single(ext_descriptor) .network(network) .create_wallet(persister) - .map_err(|_| Error::Generic("Can't create wallet.".to_string()))?; + .map_err(|e| Error::Generic(e.to_string()))?; Ok(wallet) } _ => Err(Error::Generic( -- 2.49.0