Skip to content

Commit

Permalink
refactor(wallet): use walleterror for error handling in try_get_inter…
Browse files Browse the repository at this point in the history
…nal_address
  • Loading branch information
reez committed Apr 2, 2024
1 parent 9a996b1 commit c6d17b7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
7 changes: 6 additions & 1 deletion bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ interface WalletCreationError {
LoadedNetworkDoesNotMatch(Network expected, Network? got);
};

[Error]
interface PersistenceError {
Write(string e);
};

[Error]
interface EsploraError {
Ureq(string error_message);
Expand Down Expand Up @@ -118,7 +123,7 @@ interface Wallet {

AddressInfo get_address(AddressIndex address_index);

[Throws=Alpha3Error]
[Throws=PersistenceError]
AddressInfo try_get_internal_address(AddressIndex address_index);

Network network();
Expand Down
28 changes: 27 additions & 1 deletion bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ pub enum WalletCreationError {
},
}

#[derive(Debug, thiserror::Error)]
pub enum PersistenceError {
#[error("writing to persistence error: {e}")]
Write { e: String },
}

#[derive(Debug, thiserror::Error)]
pub enum EsploraError {
#[error("ureq error: {error_message}")]
Expand Down Expand Up @@ -123,6 +129,14 @@ impl From<NewOrLoadError<std::io::Error, IterError>> for WalletCreationError {
}
}

impl From<std::io::Error> for PersistenceError {
fn from(error: std::io::Error) -> Self {
PersistenceError::Write {
e: error.to_string(),
}
}
}

impl From<DescriptorError> for Alpha3Error {
fn from(_: DescriptorError) -> Self {
Alpha3Error::Generic
Expand Down Expand Up @@ -216,7 +230,7 @@ impl From<BdkEsploraError> for EsploraError {

#[cfg(test)]
mod test {
use crate::error::EsploraError;
use crate::error::{EsploraError, PersistenceError};
use crate::CalculateFeeError;
use crate::OutPoint;

Expand Down Expand Up @@ -317,4 +331,16 @@ mod test {
assert_eq!(error.to_string(), expected_message);
}
}

#[test]
fn test_persistence_error() {
let io_err = std::io::Error::new(
std::io::ErrorKind::Other,
"unable to persist the new address",
);
let op_err: PersistenceError = io_err.into();

let PersistenceError::Write { e } = op_err;
assert_eq!(e, "unable to persist the new address");
}
}
1 change: 1 addition & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use crate::wallet::TxBuilder;
use crate::wallet::Update;
use crate::wallet::Wallet;

use crate::error::PersistenceError;
use crate::error::WalletCreationError;
use bdk::bitcoin::Network;
use bdk::keys::bip39::WordCount;
Expand Down
15 changes: 7 additions & 8 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{Alpha3Error, CalculateFeeError, WalletCreationError};
use crate::error::{Alpha3Error, CalculateFeeError, PersistenceError, WalletCreationError};
use crate::types::ScriptAmount;
use crate::types::{Balance, FeeRate};
use crate::Script;
Expand Down Expand Up @@ -67,13 +67,12 @@ impl Wallet {
pub fn try_get_internal_address(
&self,
address_index: AddressIndex,
) -> Result<AddressInfo, Alpha3Error> {
self.get_wallet()
.try_get_internal_address(address_index.into())
.map_or_else(
|_| Err(Alpha3Error::Generic),
|address_info| Ok(address_info.into()),
)
) -> Result<AddressInfo, PersistenceError> {
let address_info = self
.get_wallet()
.try_get_internal_address(address_index.into())?
.into();
Ok(address_info)
}

pub fn network(&self) -> Network {
Expand Down

0 comments on commit c6d17b7

Please sign in to comment.