refactor(wallet): use walleterror for error handling in try_get_internal_address

This commit is contained in:
Matthew 2024-02-26 11:12:36 -06:00
parent 9a996b1a94
commit c6d17b77ec
No known key found for this signature in database
GPG Key ID: 8D4FCD82DD54DDD2
4 changed files with 41 additions and 10 deletions

View File

@ -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();

View File

@ -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<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
@ -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;
@ -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");
}
}

View File

@ -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;

View File

@ -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<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 {