Skip to content

Commit

Permalink
feat: add sqlite error
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Jun 4, 2024
1 parent 19b4e11 commit ea0f0ab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
6 changes: 6 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ interface SignerError {
External(string error_message);
};

[Error]
interface SqliteError {
InvalidNetwork(Network expected, Network given);
Sqlite(string rusqlite_error);
};

[Error]
interface TransactionError {
Io();
Expand Down
42 changes: 25 additions & 17 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::bitcoin::OutPoint;
use crate::Network;

use bdk_electrum::electrum_client::Error as BdkElectrumError;
use bdk_esplora::esplora_client::{Error as BdkEsploraError, Error};
use bdk_sqlite::rusqlite::Error as BdkSqliteError;
use bdk_sqlite::Error as BdkSqliteError;
use bdk_sqlite::rusqlite::Error as BdkRusqliteError;
use bdk_wallet::bitcoin::address::Error as BdkAddressError;
use bdk_wallet::bitcoin::address::ParseError;
use bdk_wallet::bitcoin::amount::ParseAmountError as BdkParseAmountError;
Expand All @@ -11,7 +13,6 @@ use bdk_wallet::bitcoin::consensus::encode::Error as BdkEncodeError;
use bdk_wallet::bitcoin::psbt::Error as BdkPsbtError;
use bdk_wallet::bitcoin::psbt::ExtractTxError as BdkExtractTxError;
use bdk_wallet::bitcoin::psbt::PsbtParseError as BdkPsbtParseError;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::chain::local_chain::CannotConnectError as BdkCannotConnectError;
use bdk_wallet::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
use bdk_wallet::descriptor::DescriptorError as BdkDescriptorError;
Expand Down Expand Up @@ -575,6 +576,17 @@ pub enum SignerError {
External { error_message: String },
}

#[derive(Debug, thiserror::Error)]
pub enum SqliteError {
// This error is renamed from Network to InvalidNetwork to avoid conflict with the Network enum
// in uniffi.
#[error("invalid network, cannot change the one already stored in the database")]
InvalidNetwork { expected: Network, given: Network },

#[error("SQLite error: {rusqlite_error}")]
Sqlite { rusqlite_error: String },
}

#[derive(Debug, thiserror::Error)]
pub enum TransactionError {
#[error("io error")]
Expand Down Expand Up @@ -1211,27 +1223,23 @@ impl From<BdkEncodeError> for TransactionError {
}
}

// impl From<BdkFileError> for WalletCreationError {
// fn from(error: BdkFileError) -> Self {
// match error {
// BdkFileError::Io(e) => WalletCreationError::Io {
// error_message: e.to_string(),
// },
// BdkFileError::InvalidMagicBytes { got, expected } => {
// WalletCreationError::InvalidMagicBytes { got, expected }
// }
// }
// }
// }

impl From<BdkSqliteError> for WalletCreationError {
fn from(error: BdkSqliteError) -> Self {
impl From<BdkRusqliteError> for WalletCreationError {
fn from(error: BdkRusqliteError) -> Self {
WalletCreationError::Sqlite {
error_message: error.to_string(),
}
}
}

impl From<BdkSqliteError> for SqliteError {
fn from(error: BdkSqliteError) -> Self {
match error {
BdkSqliteError::Network { expected, given } => SqliteError::InvalidNetwork { expected, given },
BdkSqliteError::Sqlite(e) => SqliteError::Sqlite { rusqlite_error: e.to_string() },
}
}
}

impl From<NewOrLoadError> for WalletCreationError {
fn from(error: NewOrLoadError) -> Self {
match error {
Expand Down
1 change: 1 addition & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use crate::error::PersistenceError;
use crate::error::PsbtError;
use crate::error::PsbtParseError;
use crate::error::SignerError;
use crate::error::SqliteError;
use crate::error::TransactionError;
use crate::error::TxidParseError;
use crate::error::WalletCreationError;
Expand Down

0 comments on commit ea0f0ab

Please sign in to comment.