]> Untitled Git - bdk-cli/commitdiff
refactor: fix error handling
authorVihiga Tyonum <withtvpeter@gmail.com>
Sat, 8 Mar 2025 03:46:22 +0000 (04:46 +0100)
committerVihiga Tyonum <withtvpeter@gmail.com>
Wed, 12 Mar 2025 13:56:43 +0000 (14:56 +0100)
-add more error types to error enum
- update the handlers file to replace Generic
error arm

[Ticket: X]

ci/test_features.sh
src/commands.rs
src/error.rs
src/handlers.rs
src/utils.rs

index 74078182210313ffbf2d7eb177de75b4dbb0be02..ecef034f8fc948ccde6dab34dce5d8b726ac9362 100755 (executable)
@@ -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"
 )
index 9d22a792ac098aa5fb61ef4fa010cea1579941d1..20468708eacff404c27ffffea7ab603a04df0c67 100644 (file)
@@ -195,8 +195,8 @@ pub struct WalletOpts {
 
     #[cfg(feature = "rpc")]
     /// Sets an optional cookie authentication.
-    #[arg(name = "COOKIE", long)]
-    pub cookie: Option<String>
+    #[clap(name = "COOKIE", long = "cookie")]
+    pub cookie: Option<String>,
 }
 
 /// Options to configure a SOCKS5 proxy for a blockchain client connection.
index 2f18854dcde51ca164054781aabd987c5cc41b73..ebacc8de8da609082f51547ba193db809d902366 100644 (file)
@@ -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),
 }
index 0e58cbd9043c60e7f0c970eb3954207e17f5c349..13c3fe16a508c6b45d5a23575b9a915f3ba526dc 100644 (file)
@@ -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);
                 }
             }
index cb6ae474787303c96631e212ca44882bf829b4c7..91ccec6a86a10a7fd59a05e2d2148754fa89ffa7 100644 (file)
@@ -192,7 +192,10 @@ pub(crate) fn new_persisted_wallet<P: WalletPersister>(
     network: Network,
     persister: &mut P,
     wallet_opts: &WalletOpts,
-) -> Result<PersistedWallet<P>, Error> {
+) -> Result<PersistedWallet<P>, 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<P: WalletPersister>(
                 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(