diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index aa8a3ab..bf779c1 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -27,6 +27,11 @@ interface WalletCreationError { LoadedNetworkDoesNotMatch(Network expected, Network? got); }; +[Error] +interface PersistenceError { + Write(string e); +}; + [Error] interface EsploraError { Ureq(string error_message); @@ -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(); diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index cb3c0c0..9fddae9 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -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}")] @@ -123,6 +129,14 @@ impl From> for WalletCreationError { } } +impl From for PersistenceError { + fn from(error: std::io::Error) -> Self { + PersistenceError::Write { + e: error.to_string(), + } + } +} + impl From for Alpha3Error { fn from(_: DescriptorError) -> Self { Alpha3Error::Generic @@ -216,7 +230,7 @@ impl From for EsploraError { #[cfg(test)] mod test { - use crate::error::EsploraError; + use crate::error::{EsploraError, PersistenceError}; use crate::CalculateFeeError; use crate::OutPoint; @@ -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"); + } } diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 31828e3..e4496d3 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -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; diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index 8a21f4c..4401e1b 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -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; @@ -67,13 +67,12 @@ impl Wallet { pub fn try_get_internal_address( &self, address_index: AddressIndex, - ) -> Result { - self.get_wallet() - .try_get_internal_address(address_index.into()) - .map_or_else( - |_| Err(Alpha3Error::Generic), - |address_info| Ok(address_info.into()), - ) + ) -> Result { + let address_info = self + .get_wallet() + .try_get_internal_address(address_index.into())? + .into(); + Ok(address_info) } pub fn network(&self) -> Network {