Skip to content

Commit c163b95

Browse files
committed
refactor: fix error handling
-add more error types to error.rs - update the handlers file to replace Generic type [Ticket: X]
1 parent 8307db0 commit c163b95

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

src/commands.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ pub struct WalletOpts {
195195

196196
#[cfg(feature = "rpc")]
197197
/// Sets an optional cookie authentication.
198-
#[arg(name = "COOKIE", long)]
199-
pub cookie: Option<String>
198+
#[clap(name = "COOKIE", long = "cookie")]
199+
pub cookie: Option<String>,
200200
}
201201

202202
/// Options to configure a SOCKS5 proxy for a blockchain client connection.

src/error.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use thiserror::Error;
44

55
#[derive(Debug, Error)]
66
pub enum BDKCliError {
7-
87
#[error("BIP39 error: {0}")]
98
BIP39Error(#[from] bdk_wallet::bip39::Error),
109

@@ -39,6 +38,9 @@ pub enum BDKCliError {
3938
#[error("Key error: {0}")]
4039
KeyError(#[from] bdk_wallet::keys::KeyError),
4140

41+
#[error("LocalChain error: {0}")]
42+
LocalChainError(#[from] bdk_wallet::chain::local_chain::ApplyHeaderError),
43+
4244
#[error("Miniscript error: {0}")]
4345
MiniscriptError(#[from] bdk_wallet::miniscript::Error),
4446

@@ -79,4 +81,8 @@ pub enum BDKCliError {
7981

8082
#[error("Consensus decoding error: {0}")]
8183
Hex(#[from] HexToBytesError),
84+
85+
#[cfg(feature = "rpc")]
86+
#[error("RPC error: {0}")]
87+
BitcoinCoreRpcError(#[from] bdk_bitcoind_rpc::bitcoincore_rpc::Error),
8288
}

src/handlers.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ use {crate::utils::BlockchainClient::Esplora, bdk_esplora::EsploraAsyncExt};
5858
#[cfg(feature = "rpc")]
5959
use {
6060
crate::utils::BlockchainClient::RpcClient,
61-
bdk_bitcoind_rpc::{Emitter, bitcoincore_rpc::RpcApi},
61+
bdk_bitcoind_rpc::{bitcoincore_rpc::RpcApi, Emitter},
6262
bdk_wallet::chain::{BlockId, CheckPoint},
6363
};
6464

@@ -361,9 +361,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
361361
client
362362
.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
363363

364-
let update = client
365-
.full_scan(request, stop_gap, batch_size, false)
366-
.map_err(|e| Error::Generic(e.to_string()))?;
364+
let update = client.full_scan(request, stop_gap, batch_size, false)?;
367365
wallet.apply_update(update)?;
368366
}
369367
#[cfg(feature = "esplora")]
@@ -374,7 +372,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
374372
let update = client
375373
.full_scan(request, stop_gap, parallel_requests)
376374
.await
377-
.map_err(|e| Error::Generic(e.to_string()))?;
375+
.map_err(|e| *e)?;
378376
wallet.apply_update(update)?;
379377
}
380378

@@ -389,20 +387,15 @@ pub(crate) async fn handle_online_wallet_subcommand(
389387
let mut emitter =
390388
Emitter::new(&client, genesis_cp.clone(), genesis_cp.height());
391389

392-
while let Some(block_event) = emitter
393-
.next_block()
394-
.map_err(|e| Error::Generic(e.to_string()))?
395-
{
396-
wallet
397-
.apply_block_connected_to(
398-
&block_event.block,
399-
block_event.block_height(),
400-
block_event.connected_to(),
401-
)
402-
.map_err(|e| Error::Generic(e.to_string()))?;
390+
while let Some(block_event) = emitter.next_block()? {
391+
wallet.apply_block_connected_to(
392+
&block_event.block,
393+
block_event.block_height(),
394+
block_event.connected_to(),
395+
)?;
403396
}
404397

405-
let mempool_txs = emitter.mempool().unwrap();
398+
let mempool_txs = emitter.mempool()?;
406399
wallet.apply_unconfirmed_txs(mempool_txs);
407400
}
408401
}
@@ -423,9 +416,7 @@ pub(crate) async fn handle_online_wallet_subcommand(
423416
client
424417
.populate_tx_cache(wallet.tx_graph().full_txs().map(|tx_node| tx_node.tx));
425418

426-
let update = client
427-
.sync(request, batch_size, false)
428-
.map_err(|e| Error::Generic(e.to_string()))?;
419+
let update = client.sync(request, batch_size, false)?;
429420
wallet.apply_update(update)?;
430421
}
431422
#[cfg(feature = "esplora")]
@@ -436,28 +427,23 @@ pub(crate) async fn handle_online_wallet_subcommand(
436427
let update = client
437428
.sync(request, parallel_requests)
438429
.await
439-
.map_err(|e| Error::Generic(e.to_string()))?;
430+
.map_err(|e| *e)?;
440431
wallet.apply_update(update)?;
441432
}
442433
#[cfg(feature = "rpc")]
443434
RpcClient { client } => {
444435
let wallet_cp = wallet.latest_checkpoint();
445436
let mut emitter = Emitter::new(&client, wallet_cp.clone(), wallet_cp.height());
446437

447-
while let Some(block_event) = emitter
448-
.next_block()
449-
.map_err(|e| Error::Generic(e.to_string()))?
450-
{
451-
wallet
452-
.apply_block_connected_to(
453-
&block_event.block,
454-
block_event.block_height(),
455-
block_event.connected_to(),
456-
)
457-
.map_err(|e| Error::Generic(e.to_string()))?;
438+
while let Some(block_event) = emitter.next_block()? {
439+
wallet.apply_block_connected_to(
440+
&block_event.block,
441+
block_event.block_height(),
442+
block_event.connected_to(),
443+
)?;
458444
}
459445

460-
let mempool_txs = emitter.mempool().unwrap();
446+
let mempool_txs = emitter.mempool()?;
461447
wallet.apply_unconfirmed_txs(mempool_txs);
462448
}
463449
}

0 commit comments

Comments
 (0)