diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index baedb49e..6e44a749 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -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(); diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index b6381520..36978d30 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -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::rusqlite::Error as BdkRusqliteError; +use bdk_sqlite::Error as BdkSqliteError; use bdk_wallet::bitcoin::address::Error as BdkAddressError; use bdk_wallet::bitcoin::address::ParseError; use bdk_wallet::bitcoin::amount::ParseAmountError as BdkParseAmountError; @@ -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; @@ -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")] @@ -1211,27 +1223,27 @@ impl From for TransactionError { } } -// impl From 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 for WalletCreationError { - fn from(error: BdkSqliteError) -> Self { +impl From for WalletCreationError { + fn from(error: BdkRusqliteError) -> Self { WalletCreationError::Sqlite { error_message: error.to_string(), } } } +impl From 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 for WalletCreationError { fn from(error: NewOrLoadError) -> Self { match error { diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 5624645f..da1faf3a 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -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;