Skip to content

Commit 67178e7

Browse files
committed
refactor(wallet)!: move wallet errors to wallet::error module, remove impl_error macro
1 parent 2cac919 commit 67178e7

File tree

8 files changed

+57
-35
lines changed

8 files changed

+57
-35
lines changed

crates/bdk/src/descriptor/error.rs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,38 @@ impl fmt::Display for Error {
8787
#[cfg(feature = "std")]
8888
impl std::error::Error for Error {}
8989

90-
impl_error!(bitcoin::bip32::Error, Bip32);
91-
impl_error!(bitcoin::base58::Error, Base58);
92-
impl_error!(bitcoin::key::Error, Pk);
93-
impl_error!(miniscript::Error, Miniscript);
94-
impl_error!(bitcoin::hashes::hex::Error, Hex);
95-
impl_error!(crate::descriptor::policy::PolicyError, Policy);
90+
impl From<bitcoin::bip32::Error> for Error {
91+
fn from(err: bitcoin::bip32::Error) -> Self {
92+
Error::Bip32(err)
93+
}
94+
}
95+
96+
impl From<bitcoin::base58::Error> for Error {
97+
fn from(err: bitcoin::base58::Error) -> Self {
98+
Error::Base58(err)
99+
}
100+
}
101+
102+
impl From<bitcoin::key::Error> for Error {
103+
fn from(err: bitcoin::key::Error) -> Self {
104+
Error::Pk(err)
105+
}
106+
}
107+
108+
impl From<miniscript::Error> for Error {
109+
fn from(err: miniscript::Error) -> Self {
110+
Error::Miniscript(err)
111+
}
112+
}
113+
114+
impl From<bitcoin::hashes::hex::Error> for Error {
115+
fn from(err: bitcoin::hashes::hex::Error) -> Self {
116+
Error::Hex(err)
117+
}
118+
}
119+
120+
impl From<crate::descriptor::policy::PolicyError> for Error {
121+
fn from(err: crate::descriptor::policy::PolicyError) -> Self {
122+
Error::Policy(err)
123+
}
124+
}

crates/bdk/src/keys/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,8 +932,17 @@ pub enum KeyError {
932932
Miniscript(miniscript::Error),
933933
}
934934

935-
impl_error!(miniscript::Error, Miniscript, KeyError);
936-
impl_error!(bitcoin::bip32::Error, Bip32, KeyError);
935+
impl From<miniscript::Error> for KeyError {
936+
fn from(err: miniscript::Error) -> Self {
937+
KeyError::Miniscript(err)
938+
}
939+
}
940+
941+
impl From<bip32::Error> for KeyError {
942+
fn from(err: bip32::Error) -> Self {
943+
KeyError::Bip32(err)
944+
}
945+
}
937946

938947
impl fmt::Display for KeyError {
939948
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

crates/bdk/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ extern crate serde_json;
2727
#[cfg(feature = "keys-bip39")]
2828
extern crate bip39;
2929

30-
#[macro_use]
31-
pub mod error;
3230
pub mod descriptor;
3331
pub mod keys;
3432
pub mod psbt;

crates/bdk/src/wallet/coin_selection.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
//! # use std::str::FromStr;
2828
//! # use bitcoin::*;
2929
//! # use bdk::wallet::{self, ChangeSet, coin_selection::*, coin_selection};
30-
//! # use bdk::error::CreateTxError;
30+
//! # use bdk::wallet::error::CreateTxError;
3131
//! # use bdk_chain::PersistBackend;
3232
//! # use bdk::*;
3333
//! # use bdk::wallet::coin_selection::decide_change;

crates/bdk/src/error.rs renamed to crates/bdk/src/wallet/error.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,6 @@ impl fmt::Display for MiniscriptPsbtError {
4343
#[cfg(feature = "std")]
4444
impl std::error::Error for MiniscriptPsbtError {}
4545

46-
macro_rules! impl_error {
47-
( $from:ty, $to:ident ) => {
48-
impl_error!($from, $to, Error);
49-
};
50-
( $from:ty, $to:ident, $impl_for:ty ) => {
51-
impl core::convert::From<$from> for $impl_for {
52-
fn from(err: $from) -> Self {
53-
<$impl_for>::$to(err)
54-
}
55-
}
56-
};
57-
}
58-
5946
#[derive(Debug)]
6047
/// Error returned from [`TxBuilder::finish`]
6148
///
@@ -259,8 +246,6 @@ impl<P> From<coin_selection::Error> for CreateTxError<P> {
259246
#[cfg(feature = "std")]
260247
impl<P: core::fmt::Display + core::fmt::Debug> std::error::Error for CreateTxError<P> {}
261248

262-
//
263-
264249
#[derive(Debug)]
265250
/// Error returned from [`Wallet::build_fee_bump`]
266251
///

crates/bdk/src/wallet/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub mod signer;
5151
pub mod tx_builder;
5252
pub(crate) mod utils;
5353

54+
pub mod error;
5455
#[cfg(feature = "hardware-signer")]
5556
#[cfg_attr(docsrs, doc(cfg(feature = "hardware-signer")))]
5657
pub mod hardwaresigner;
@@ -69,11 +70,11 @@ use crate::descriptor::{
6970
calc_checksum, into_wallet_descriptor_checked, DerivedDescriptor, DescriptorMeta,
7071
ExtendedDescriptor, ExtractPolicy, IntoWalletDescriptor, Policy, XKeyUtils,
7172
};
72-
use crate::error::{BuildFeeBumpError, CreateTxError, MiniscriptPsbtError, SignError};
7373
use crate::psbt::PsbtUtils;
7474
use crate::signer::SignerError;
7575
use crate::types::*;
7676
use crate::wallet::coin_selection::Excess::{Change, NoChange};
77+
use crate::wallet::error::{BuildFeeBumpError, CreateTxError, MiniscriptPsbtError, SignError};
7778

7879
const COINBASE_MATURITY: u32 = 100;
7980

@@ -1126,7 +1127,7 @@ impl<D> Wallet<D> {
11261127
/// # use bitcoin::*;
11271128
/// # use bdk::*;
11281129
/// # use bdk::wallet::ChangeSet;
1129-
/// # use bdk::error::CreateTxError;
1130+
/// # use bdk::wallet::error::CreateTxError;
11301131
/// # use bdk_chain::PersistBackend;
11311132
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
11321133
/// # let mut wallet = doctest_wallet!();
@@ -1526,7 +1527,7 @@ impl<D> Wallet<D> {
15261527
/// # use bitcoin::*;
15271528
/// # use bdk::*;
15281529
/// # use bdk::wallet::ChangeSet;
1529-
/// # use bdk::error::CreateTxError;
1530+
/// # use bdk::wallet::error::CreateTxError;
15301531
/// # use bdk_chain::PersistBackend;
15311532
/// # use anyhow::Error;
15321533
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
@@ -1700,7 +1701,7 @@ impl<D> Wallet<D> {
17001701
/// # use bitcoin::*;
17011702
/// # use bdk::*;
17021703
/// # use bdk::wallet::ChangeSet;
1703-
/// # use bdk::error::{CreateTxError, SignError};
1704+
/// # use bdk::wallet::error::{CreateTxError, SignError};
17041705
/// # use bdk_chain::PersistBackend;
17051706
/// # let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)";
17061707
/// # let mut wallet = doctest_wallet!();

crates/bdk/src/wallet/tx_builder.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
//! # use bitcoin::*;
1919
//! # use bdk::*;
2020
//! # use bdk::wallet::ChangeSet;
21-
//! # use bdk::error::CreateTxError;
21+
//! # use bdk::wallet::error::CreateTxError;
2222
//! # use bdk::wallet::tx_builder::CreateTx;
2323
//! # let to_address = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
2424
//! # use bdk_chain::PersistBackend;
@@ -51,8 +51,8 @@ use bitcoin::{absolute, script::PushBytes, OutPoint, ScriptBuf, Sequence, Transa
5151

5252
use super::coin_selection::{CoinSelectionAlgorithm, DefaultCoinSelectionAlgorithm};
5353
use super::ChangeSet;
54-
use crate::error::{AddForeignUtxoError, AddUtxoError, AllowShrinkingError};
5554
use crate::types::{FeeRate, KeychainKind, LocalUtxo, WeightedUtxo};
55+
use crate::wallet::error::{AddForeignUtxoError, AddUtxoError, AllowShrinkingError};
5656
use crate::wallet::CreateTxError;
5757
use crate::{Utxo, Wallet};
5858

@@ -85,7 +85,7 @@ impl TxBuilderContext for BumpFee {}
8585
/// # use bitcoin::*;
8686
/// # use core::str::FromStr;
8787
/// # use bdk::wallet::ChangeSet;
88-
/// # use bdk::error::CreateTxError;
88+
/// # use bdk::wallet::error::CreateTxError;
8989
/// # use bdk_chain::PersistBackend;
9090
/// # let mut wallet = doctest_wallet!();
9191
/// # let addr1 = Address::from_str("2N4eQYCbKUHCCTUjBJeHcJp9ok6J2GZsTDt").unwrap().assume_checked();
@@ -652,7 +652,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm> TxBuilder<'a, D, Cs, CreateTx> {
652652
/// # use bitcoin::*;
653653
/// # use bdk::*;
654654
/// # use bdk::wallet::ChangeSet;
655-
/// # use bdk::error::CreateTxError;
655+
/// # use bdk::wallet::error::CreateTxError;
656656
/// # use bdk::wallet::tx_builder::CreateTx;
657657
/// # use bdk_chain::PersistBackend;
658658
/// # let to_address =

crates/bdk/tests/wallet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use std::str::FromStr;
22

33
use assert_matches::assert_matches;
44
use bdk::descriptor::calc_checksum;
5-
use bdk::error::{AddForeignUtxoError, CreateTxError, SignError};
65
use bdk::psbt::PsbtUtils;
76
use bdk::signer::{SignOptions, SignerError};
87
use bdk::wallet::coin_selection::{self, LargestFirstCoinSelection};
8+
use bdk::wallet::error::{AddForeignUtxoError, CreateTxError, SignError};
99
use bdk::wallet::AddressIndex::*;
1010
use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet};
1111
use bdk::{FeeRate, KeychainKind};

0 commit comments

Comments
 (0)