Skip to content

Commit d7bfd34

Browse files
committed
refactor: remove PersisterError from BDKCliError
And add individual database errors instead. This makes error handling cleaner as suggested by tvpeter.
1 parent 7a92039 commit d7bfd34

File tree

4 files changed

+24
-40
lines changed

4 files changed

+24
-40
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/error.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#[cfg(any(feature = "sqlite", feature = "redb"))]
2-
use crate::persister::PersisterError;
31
use bdk_wallet::bitcoin::hex::HexToBytesError;
42
use bdk_wallet::bitcoin::psbt::ExtractTxError;
53
use bdk_wallet::bitcoin::{base64, consensus};
@@ -59,9 +57,13 @@ pub enum BDKCliError {
5957
#[error("PsbtError: {0}")]
6058
PsbtError(#[from] bdk_wallet::bitcoin::psbt::Error),
6159

62-
#[cfg(any(feature = "redb", feature = "sqlite"))]
63-
#[error("Persistence error: {0}")]
64-
PersistenceError(#[from] PersisterError),
60+
#[cfg(feature = "sqlite")]
61+
#[error("Rusqlite error: {0}")]
62+
RusqliteError(#[from] bdk_wallet::rusqlite::Error),
63+
64+
#[cfg(feature = "redb")]
65+
#[error("RedbStore error: {0}")]
66+
RedbStoreError(#[from] bdk_redb::error::StoreError),
6567

6668
#[error("Serde json error: {0}")]
6769
SerdeJson(#[from] serde_json::Error),

src/handlers.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::commands::OfflineWalletSubCommand::*;
1414
use crate::commands::*;
1515
use crate::error::BDKCliError as Error;
1616
#[cfg(any(feature = "sqlite", feature = "redb"))]
17-
use crate::persister::{Persister, PersisterError};
17+
use crate::persister::Persister;
1818
#[cfg(feature = "cbf")]
1919
use crate::utils::BlockchainClient::KyotoClient;
2020
use crate::utils::*;
@@ -764,23 +764,20 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
764764
#[cfg(feature = "sqlite")]
765765
DatabaseType::Sqlite => {
766766
let db_file = database_path.join("wallet.sqlite");
767-
let connection = Connection::open(db_file).map_err(PersisterError::from)?;
767+
let connection = Connection::open(db_file)?;
768768
log::debug!("Sqlite database opened successfully");
769769
Persister::Connection(connection)
770770
}
771771
#[cfg(feature = "redb")]
772772
DatabaseType::Redb => {
773773
let db = Arc::new(
774774
bdk_redb::redb::Database::create(database_path.join("wallet.redb"))
775-
.map_err(bdk_redb::redb::Error::from)
776-
.map_err(bdk_redb::error::StoreError::from)
777-
.map_err(PersisterError::from)?,
775+
.map_err(bdk_redb::error::StoreError::from)?,
778776
);
779777
let store = RedbStore::new(
780778
db,
781779
wallet_name.as_deref().unwrap_or("wallet1").to_string(),
782-
)
783-
.map_err(PersisterError::from)?;
780+
)?;
784781
log::debug!("Redb database opened successfully");
785782
Persister::RedbStore(store)
786783
}
@@ -824,23 +821,20 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
824821
#[cfg(feature = "sqlite")]
825822
DatabaseType::Sqlite => {
826823
let db_file = database_path.join("wallet.sqlite");
827-
let connection = Connection::open(db_file).map_err(PersisterError::from)?;
824+
let connection = Connection::open(db_file)?;
828825
log::debug!("Sqlite database opened successfully");
829826
Persister::Connection(connection)
830827
}
831828
#[cfg(feature = "redb")]
832829
DatabaseType::Redb => {
833830
let db = Arc::new(
834831
bdk_redb::redb::Database::create(database_path.join("wallet.redb"))
835-
.map_err(bdk_redb::redb::Error::from)
836-
.map_err(bdk_redb::error::StoreError::from)
837-
.map_err(PersisterError::from)?,
832+
.map_err(bdk_redb::error::StoreError::from)?,
838833
);
839834
let store = RedbStore::new(
840835
db,
841836
wallet_name.as_deref().unwrap_or("wallet1").to_string(),
842-
)
843-
.map_err(PersisterError::from)?;
837+
)?;
844838
log::debug!("Redb database opened successfully");
845839
Persister::RedbStore(store)
846840
}
@@ -891,23 +885,20 @@ pub(crate) async fn handle_command(cli_opts: CliOpts) -> Result<String, Error> {
891885
#[cfg(feature = "sqlite")]
892886
DatabaseType::Sqlite => {
893887
let db_file = database_path.join("wallet.sqlite");
894-
let connection = Connection::open(db_file).map_err(PersisterError::from)?;
888+
let connection = Connection::open(db_file)?;
895889
log::debug!("Sqlite database opened successfully");
896890
Persister::Connection(connection)
897891
}
898892
#[cfg(feature = "redb")]
899893
DatabaseType::Redb => {
900894
let db = Arc::new(
901895
bdk_redb::redb::Database::create(database_path.join("wallet.redb"))
902-
.map_err(bdk_redb::redb::Error::from)
903-
.map_err(bdk_redb::error::StoreError::from)
904-
.map_err(PersisterError::from)?,
896+
.map_err(bdk_redb::error::StoreError::from)?,
905897
);
906898
let store = RedbStore::new(
907899
db,
908900
wallet_name.as_deref().unwrap_or("wallet1").to_string(),
909-
)
910-
.map_err(PersisterError::from)?;
901+
)?;
911902
log::debug!("Redb database opened successfully");
912903
Persister::RedbStore(store)
913904
}

src/persister.rs

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::BDKCliError;
12
use bdk_wallet::WalletPersister;
23

34
pub enum Persister {
@@ -7,28 +8,18 @@ pub enum Persister {
78
RedbStore(bdk_redb::Store),
89
}
910

10-
#[derive(Debug, thiserror::Error)]
11-
pub enum PersisterError {
12-
#[cfg(feature = "sqlite")]
13-
#[error("Sqlite error: {0}")]
14-
SqliteError(#[from] bdk_wallet::rusqlite::Error),
15-
#[cfg(feature = "redb")]
16-
#[error("Redb error: {0}")]
17-
RedbError(#[from] bdk_redb::error::StoreError),
18-
}
19-
2011
impl WalletPersister for Persister {
21-
type Error = PersisterError;
12+
type Error = BDKCliError;
2213

2314
fn initialize(persister: &mut Self) -> Result<bdk_wallet::ChangeSet, Self::Error> {
2415
match persister {
2516
#[cfg(feature = "sqlite")]
2617
Persister::Connection(connection) => {
27-
WalletPersister::initialize(connection).map_err(PersisterError::from)
18+
WalletPersister::initialize(connection).map_err(BDKCliError::from)
2819
}
2920
#[cfg(feature = "redb")]
3021
Persister::RedbStore(store) => {
31-
WalletPersister::initialize(store).map_err(PersisterError::from)
22+
WalletPersister::initialize(store).map_err(BDKCliError::from)
3223
}
3324
}
3425
}
@@ -37,11 +28,11 @@ impl WalletPersister for Persister {
3728
match persister {
3829
#[cfg(feature = "sqlite")]
3930
Persister::Connection(connection) => {
40-
WalletPersister::persist(connection, changeset).map_err(PersisterError::from)
31+
WalletPersister::persist(connection, changeset).map_err(BDKCliError::from)
4132
}
4233
#[cfg(feature = "redb")]
4334
Persister::RedbStore(store) => {
44-
WalletPersister::persist(store, changeset).map_err(PersisterError::from)
35+
WalletPersister::persist(store, changeset).map_err(BDKCliError::from)
4536
}
4637
}
4738
}

0 commit comments

Comments
 (0)