Skip to content

Commit

Permalink
temp: use thiserror for the WalletError type
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Mar 20, 2024
1 parent 6d4513e commit f2d4545
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 46 deletions.
2 changes: 1 addition & 1 deletion bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface PersistBackendError {

[Error]
interface WalletError {
Io(string e);
Io();
InvalidMagicBytes(sequence<u8> got, sequence<u8> expected);
Descriptor();
Write();
Expand Down
114 changes: 70 additions & 44 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bdk::descriptor::DescriptorError;
use bdk::wallet::error::{BuildFeeBumpError, CreateTxError};
use bdk::wallet::tx_builder::{AddUtxoError, AllowShrinkingError};
use bdk::wallet::{NewError, NewOrLoadError};
use bdk_file_store::{FileError as BdkFileError};
use bdk_file_store::FileError as BdkFileError;
use bdk_file_store::IterError;
use std::convert::Infallible;

Expand All @@ -29,62 +29,90 @@ impl fmt::Display for Alpha3Error {

impl std::error::Error for Alpha3Error {}

#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum WalletError {
// Errors coming from the FileError enum
Io {
e: String,
},
InvalidMagicBytes {
got: Vec<u8>,
expected: Vec<u8>,
},
#[error("IO error, this may mean that the file is too short")]
Io,

#[error("file has invalid magic bytes: expected={expected:?} got={got:?}")]
InvalidMagicBytes { got: Vec<u8>, expected: Vec<u8> },

// Errors coming from the NewOrLoadError enum
#[error("error with descriptor")]
Descriptor,

#[error("failed to write to persistence")]
Write,

#[error("failed to load from persistence")]
Load,

#[error("wallet is not initialized, persistence backend is empty")]
NotInitialized,

#[error("loaded genesis hash does not match the expected one")]
LoadedGenesisDoesNotMatch,

#[error("loaded network type is not {expected}, got {got:?}")]
LoadedNetworkDoesNotMatch {
expected: Network,
got: Option<Network>,
},
}

impl fmt::Display for WalletError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io { e } => write!(f, "io error trying to read file: {}", e),
Self::InvalidMagicBytes { got, expected } => write!(
f,
"file has invalid magic bytes: expected={:?} got={:?}",
expected, got,
),
Self::Descriptor => write!(f, "error with descriptor"),
Self::Write => write!(f, "failed to write to persistence"),
Self::Load => write!(f, "failed to load from persistence"),
Self::NotInitialized => {
write!(f, "wallet is not initialized, persistence backend is empty")
}
Self::LoadedGenesisDoesNotMatch => {
write!(f, "loaded genesis hash does not match the expected one")
}
Self::LoadedNetworkDoesNotMatch { expected, got } => {
write!(f, "loaded network type is not {}, got {:?}", expected, got)
}
}
}
}

impl std::error::Error for WalletError {}
// #[derive(Debug)]
// pub enum WalletError {
// // Errors coming from the FileError enum
// Io {
// e: String,
// },
// InvalidMagicBytes {
// got: Vec<u8>,
// expected: Vec<u8>,
// },
//
// // Errors coming from the NewOrLoadError enum
// Descriptor,
// Write,
// Load,
// NotInitialized,
// LoadedGenesisDoesNotMatch,
// LoadedNetworkDoesNotMatch {
// expected: Network,
// got: Option<Network>,
// },
// }

// impl fmt::Display for WalletError {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// match self {
// Self::Io { e } => write!(f, "io error trying to read file: {}", e),
// Self::InvalidMagicBytes { got, expected } => write!(
// f,
// "file has invalid magic bytes: expected={:?} got={:?}",
// expected, got,
// ),
// Self::Descriptor => write!(f, "error with descriptor"),
// Self::Write => write!(f, "failed to write to persistence"),
// Self::Load => write!(f, "failed to load from persistence"),
// Self::NotInitialized => {
// write!(f, "wallet is not initialized, persistence backend is empty")
// }
// Self::LoadedGenesisDoesNotMatch => {
// write!(f, "loaded genesis hash does not match the expected one")
// }
// Self::LoadedNetworkDoesNotMatch { expected, got } => {
// write!(f, "loaded network type is not {}, got {:?}", expected, got)
// }
// }
// }
// }

// impl std::error::Error for WalletError {}

impl From<BdkFileError> for WalletError {
fn from(error: BdkFileError) -> Self {
match error {
BdkFileError::Io(_) => WalletError::Io {
e: "io error trying to read file".to_string(),
},
BdkFileError::Io(_) => WalletError::Io,
BdkFileError::InvalidMagicBytes { got, expected } => {
WalletError::InvalidMagicBytes { got, expected }
}
Expand Down Expand Up @@ -383,12 +411,10 @@ mod test {

#[test]
fn test_wallet_error() {
let io_error = WalletError::Io {
e: "io error".to_string(),
};
let io_error = WalletError::Io;
assert_eq!(
io_error.to_string(),
"io error trying to read file: io error"
"IO error, this may mean that the file is too short"
);

let invalid_magic_bytes_error = WalletError::InvalidMagicBytes {
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::descriptor::Descriptor;
use crate::error::Alpha3Error;
use crate::error::CalculateFeeError;
use crate::error::EsploraError;
use crate::error::PersistBackendError;
use crate::esplora::EsploraClient;
use crate::keys::DerivationPath;
use crate::keys::DescriptorPublicKey;
Expand All @@ -32,7 +33,6 @@ use crate::wallet::SentAndReceivedValues;
use crate::wallet::TxBuilder;
use crate::wallet::Update;
use crate::wallet::Wallet;
use crate::error::PersistBackendError;

use crate::error::WalletError;
use bdk::bitcoin::Network;
Expand Down

0 comments on commit f2d4545

Please sign in to comment.