Return FfiResult errors as FfiError enum short values

This commit is contained in:
Steve Myers 2021-07-03 20:40:08 -07:00
parent 2abe7205cb
commit 6361b41f33
3 changed files with 103 additions and 54 deletions

View File

@ -1,46 +1,94 @@
use ::safer_ffi::prelude::*;
use bdk::Error; use bdk::Error;
pub fn get_name(error: &bdk::Error) -> String { #[derive_ReprC]
#[repr(u16)]
#[derive(Debug)]
pub enum FfiError {
None,
InvalidU32Bytes,
Generic,
ScriptDoesntHaveAddressForm,
SingleRecipientMultipleOutputs,
SingleRecipientNoInputs,
NoRecipients,
NoUtxosSelected,
OutputBelowDustLimit,
InsufficientFunds,
BnBTotalTriesExceeded,
BnBNoExactMatch,
UnknownUtxo,
TransactionNotFound,
TransactionConfirmed,
IrreplaceableTransaction,
FeeRateTooLow,
FeeTooLow,
MissingKeyOrigin,
Key,
ChecksumMismatch,
SpendingPolicyRequired,
InvalidPolicyPathError,
Signer,
InvalidProgressValue,
ProgressUpdateError,
InvalidOutpoint,
Descriptor,
AddressValidator,
Encode,
Miniscript,
Bip32,
Secp256k1,
Json,
Hex,
Psbt,
Electrum,
// Esplora,
// CompactFilters,
Sled,
}
impl From<&bdk::Error> for FfiError {
fn from(error: &bdk::Error) -> Self {
match error { match error {
Error::InvalidU32Bytes(_) => "InvalidU32Bytes", Error::InvalidU32Bytes(_) => FfiError::InvalidU32Bytes,
Error::Generic(_) => "Generic", Error::Generic(_) => FfiError::Generic,
Error::ScriptDoesntHaveAddressForm => "ScriptDoesntHaveAddressForm", Error::ScriptDoesntHaveAddressForm => FfiError::ScriptDoesntHaveAddressForm,
Error::SingleRecipientMultipleOutputs => "SingleRecipientMultipleOutputs", Error::SingleRecipientMultipleOutputs => FfiError::SingleRecipientMultipleOutputs,
Error::SingleRecipientNoInputs => "SingleRecipientNoInputs", Error::SingleRecipientNoInputs => FfiError::SingleRecipientNoInputs,
Error::NoRecipients => "NoRecipients", Error::NoRecipients => FfiError::NoRecipients,
Error::NoUtxosSelected => "NoUtxosSelected", Error::NoUtxosSelected => FfiError::NoUtxosSelected,
Error::OutputBelowDustLimit(_) => "OutputBelowDustLimit", Error::OutputBelowDustLimit(_) => FfiError::OutputBelowDustLimit,
Error::InsufficientFunds { .. } => "InsufficientFunds", Error::InsufficientFunds { .. } => FfiError::InsufficientFunds,
Error::BnBTotalTriesExceeded => "BnBTotalTriesExceeded", Error::BnBTotalTriesExceeded => FfiError::BnBTotalTriesExceeded,
Error::BnBNoExactMatch => "BnBNoExactMatch", Error::BnBNoExactMatch => FfiError::BnBNoExactMatch,
Error::UnknownUtxo => "UnknownUtxo", Error::UnknownUtxo => FfiError::UnknownUtxo,
Error::TransactionNotFound => "TransactionNotFound", Error::TransactionNotFound => FfiError::TransactionNotFound,
Error::TransactionConfirmed => "TransactionConfirmed", Error::TransactionConfirmed => FfiError::TransactionConfirmed,
Error::IrreplaceableTransaction => "IrreplaceableTransaction", Error::IrreplaceableTransaction => FfiError::IrreplaceableTransaction,
Error::FeeRateTooLow { .. } => "FeeRateTooLow", Error::FeeRateTooLow { .. } => FfiError::FeeRateTooLow,
Error::FeeTooLow { .. } => "FeeTooLow", Error::FeeTooLow { .. } => FfiError::FeeTooLow,
Error::MissingKeyOrigin(_) => "MissingKeyOrigin", Error::MissingKeyOrigin(_) => FfiError::MissingKeyOrigin,
Error::Key(_) => "Key", Error::Key(_) => FfiError::Key,
Error::ChecksumMismatch => "ChecksumMismatch", Error::ChecksumMismatch => FfiError::ChecksumMismatch,
Error::SpendingPolicyRequired(_) => "SpendingPolicyRequired", Error::SpendingPolicyRequired(_) => FfiError::SpendingPolicyRequired,
Error::InvalidPolicyPathError(_) => "InvalidPolicyPathError", Error::InvalidPolicyPathError(_) => FfiError::InvalidPolicyPathError,
Error::Signer(_) => "Signer", Error::Signer(_) => FfiError::Signer,
Error::InvalidProgressValue(_) => "InvalidProgressValue", Error::InvalidProgressValue(_) => FfiError::InvalidProgressValue,
Error::ProgressUpdateError => "ProgressUpdateError", Error::ProgressUpdateError => FfiError::ProgressUpdateError,
Error::InvalidOutpoint(_) => "InvalidOutpoint", Error::InvalidOutpoint(_) => FfiError::InvalidOutpoint,
Error::Descriptor(_) => "Descriptor", Error::Descriptor(_) => FfiError::Descriptor,
Error::AddressValidator(_) => "AddressValidator", Error::AddressValidator(_) => FfiError::AddressValidator,
Error::Encode(_) => "Encode", Error::Encode(_) => FfiError::Encode,
Error::Miniscript(_) => "Miniscript", Error::Miniscript(_) => FfiError::Miniscript,
Error::Bip32(_) => "Bip32", Error::Bip32(_) => FfiError::Bip32,
Error::Secp256k1(_) => "Secp256k1", Error::Secp256k1(_) => FfiError::Secp256k1,
Error::Json(_) => "Json", Error::Json(_) => FfiError::Json,
Error::Hex(_) => "Hex", Error::Hex(_) => FfiError::Hex,
Error::Psbt(_) => "Psbt", Error::Psbt(_) => FfiError::Psbt,
Error::Electrum(_) => "Electrum", Error::Electrum(_) => FfiError::Electrum,
// Error::Esplora(_) => "Esplora", // Error::Esplora(_) => JniError::Esplora,
// Error::CompactFilters(_) => "CompactFilters", // Error::CompactFilters(_) => JniError::CompactFilters,
Error::Sled(_) => "Sled", Error::Sled(_) => FfiError::Sled,
}
} }
.to_string()
} }

View File

@ -1,3 +1,4 @@
use crate::error::FfiError;
use ::safer_ffi::prelude::*; use ::safer_ffi::prelude::*;
use safer_ffi::char_p::char_p_boxed; use safer_ffi::char_p::char_p_boxed;
@ -6,14 +7,14 @@ use safer_ffi::char_p::char_p_boxed;
#[derive(Debug)] #[derive(Debug)]
pub struct FfiResult<T> { pub struct FfiResult<T> {
pub ok: T, pub ok: T,
pub err: char_p_boxed, pub err: FfiError,
} }
#[derive_ReprC] #[derive_ReprC]
#[repr(C)] #[repr(C)]
#[derive(Debug)] #[derive(Debug)]
pub struct FfiResultVoid { pub struct FfiResultVoid {
pub err: char_p_boxed, pub err: FfiError,
} }
#[ffi_export] #[ffi_export]

View File

@ -12,7 +12,7 @@ use safer_ffi::char_p::{char_p_boxed, char_p_ref};
use blockchain::BlockchainConfig; use blockchain::BlockchainConfig;
use database::DatabaseConfig; use database::DatabaseConfig;
use crate::error::get_name; use crate::error::FfiError;
use crate::types::{FfiResult, FfiResultVoid}; use crate::types::{FfiResult, FfiResultVoid};
use std::ffi::CString; use std::ffi::CString;
@ -43,11 +43,11 @@ fn new_wallet_result(
match wallet_result { match wallet_result {
Ok(w) => FfiResult { Ok(w) => FfiResult {
ok: Some(Box::new(OpaqueWallet { raw: w })), ok: Some(Box::new(OpaqueWallet { raw: w })),
err: char_p_boxed::from(CString::default()), err: FfiError::None,
}, },
Err(e) => FfiResult { Err(e) => FfiResult {
ok: None, ok: None,
err: char_p_boxed::try_from(get_name(&e)).unwrap(), err: FfiError::from(&e),
}, },
} }
} }
@ -81,10 +81,10 @@ fn sync_wallet(opaque_wallet: &OpaqueWallet) -> FfiResultVoid {
let int_result = opaque_wallet.raw.sync(log_progress(), Some(100)); let int_result = opaque_wallet.raw.sync(log_progress(), Some(100));
match int_result { match int_result {
Ok(_v) => FfiResultVoid { Ok(_v) => FfiResultVoid {
err: char_p_boxed::from(CString::default()), err: FfiError::None,
}, },
Err(e) => FfiResultVoid { Err(e) => FfiResultVoid {
err: char_p_boxed::try_from(get_name(&e)).unwrap(), err: FfiError::from(&e),
}, },
} }
} }
@ -96,11 +96,11 @@ fn new_address(opaque_wallet: &OpaqueWallet) -> FfiResult<char_p_boxed> {
match string_result { match string_result {
Ok(a) => FfiResult { Ok(a) => FfiResult {
ok: char_p_boxed::try_from(a).unwrap(), ok: char_p_boxed::try_from(a).unwrap(),
err: char_p_boxed::from(CString::default()), err: FfiError::None,
}, },
Err(e) => FfiResult { Err(e) => FfiResult {
ok: char_p_boxed::from(CString::default()), ok: char_p_boxed::from(CString::default()),
err: char_p_boxed::try_from(get_name(&e)).unwrap(), err: FfiError::from(&e),
}, },
} }
} }
@ -115,11 +115,11 @@ fn list_unspent(opaque_wallet: &OpaqueWallet) -> FfiResult<repr_c::Vec<LocalUtxo
let ve: Vec<LocalUtxo> = v.iter().map(|lu| LocalUtxo::from(lu)).collect(); let ve: Vec<LocalUtxo> = v.iter().map(|lu| LocalUtxo::from(lu)).collect();
repr_c::Vec::from(ve) repr_c::Vec::from(ve)
}, },
err: char_p_boxed::from(CString::default()), err: FfiError::None,
}, },
Err(e) => FfiResult { Err(e) => FfiResult {
ok: repr_c::Vec::EMPTY, ok: repr_c::Vec::EMPTY,
err: char_p_boxed::try_from(get_name(&e)).unwrap(), err: FfiError::from(&e),
}, },
} }
} }