Return FfiResult errors as FfiError enum short values
This commit is contained in:
parent
2abe7205cb
commit
6361b41f33
134
src/error.rs
134
src/error.rs
@ -1,46 +1,94 @@
|
||||
use ::safer_ffi::prelude::*;
|
||||
use bdk::Error;
|
||||
|
||||
pub fn get_name(error: &bdk::Error) -> String {
|
||||
match error {
|
||||
Error::InvalidU32Bytes(_) => "InvalidU32Bytes",
|
||||
Error::Generic(_) => "Generic",
|
||||
Error::ScriptDoesntHaveAddressForm => "ScriptDoesntHaveAddressForm",
|
||||
Error::SingleRecipientMultipleOutputs => "SingleRecipientMultipleOutputs",
|
||||
Error::SingleRecipientNoInputs => "SingleRecipientNoInputs",
|
||||
Error::NoRecipients => "NoRecipients",
|
||||
Error::NoUtxosSelected => "NoUtxosSelected",
|
||||
Error::OutputBelowDustLimit(_) => "OutputBelowDustLimit",
|
||||
Error::InsufficientFunds { .. } => "InsufficientFunds",
|
||||
Error::BnBTotalTriesExceeded => "BnBTotalTriesExceeded",
|
||||
Error::BnBNoExactMatch => "BnBNoExactMatch",
|
||||
Error::UnknownUtxo => "UnknownUtxo",
|
||||
Error::TransactionNotFound => "TransactionNotFound",
|
||||
Error::TransactionConfirmed => "TransactionConfirmed",
|
||||
Error::IrreplaceableTransaction => "IrreplaceableTransaction",
|
||||
Error::FeeRateTooLow { .. } => "FeeRateTooLow",
|
||||
Error::FeeTooLow { .. } => "FeeTooLow",
|
||||
Error::MissingKeyOrigin(_) => "MissingKeyOrigin",
|
||||
Error::Key(_) => "Key",
|
||||
Error::ChecksumMismatch => "ChecksumMismatch",
|
||||
Error::SpendingPolicyRequired(_) => "SpendingPolicyRequired",
|
||||
Error::InvalidPolicyPathError(_) => "InvalidPolicyPathError",
|
||||
Error::Signer(_) => "Signer",
|
||||
Error::InvalidProgressValue(_) => "InvalidProgressValue",
|
||||
Error::ProgressUpdateError => "ProgressUpdateError",
|
||||
Error::InvalidOutpoint(_) => "InvalidOutpoint",
|
||||
Error::Descriptor(_) => "Descriptor",
|
||||
Error::AddressValidator(_) => "AddressValidator",
|
||||
Error::Encode(_) => "Encode",
|
||||
Error::Miniscript(_) => "Miniscript",
|
||||
Error::Bip32(_) => "Bip32",
|
||||
Error::Secp256k1(_) => "Secp256k1",
|
||||
Error::Json(_) => "Json",
|
||||
Error::Hex(_) => "Hex",
|
||||
Error::Psbt(_) => "Psbt",
|
||||
Error::Electrum(_) => "Electrum",
|
||||
// Error::Esplora(_) => "Esplora",
|
||||
// Error::CompactFilters(_) => "CompactFilters",
|
||||
Error::Sled(_) => "Sled",
|
||||
}
|
||||
.to_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 {
|
||||
Error::InvalidU32Bytes(_) => FfiError::InvalidU32Bytes,
|
||||
Error::Generic(_) => FfiError::Generic,
|
||||
Error::ScriptDoesntHaveAddressForm => FfiError::ScriptDoesntHaveAddressForm,
|
||||
Error::SingleRecipientMultipleOutputs => FfiError::SingleRecipientMultipleOutputs,
|
||||
Error::SingleRecipientNoInputs => FfiError::SingleRecipientNoInputs,
|
||||
Error::NoRecipients => FfiError::NoRecipients,
|
||||
Error::NoUtxosSelected => FfiError::NoUtxosSelected,
|
||||
Error::OutputBelowDustLimit(_) => FfiError::OutputBelowDustLimit,
|
||||
Error::InsufficientFunds { .. } => FfiError::InsufficientFunds,
|
||||
Error::BnBTotalTriesExceeded => FfiError::BnBTotalTriesExceeded,
|
||||
Error::BnBNoExactMatch => FfiError::BnBNoExactMatch,
|
||||
Error::UnknownUtxo => FfiError::UnknownUtxo,
|
||||
Error::TransactionNotFound => FfiError::TransactionNotFound,
|
||||
Error::TransactionConfirmed => FfiError::TransactionConfirmed,
|
||||
Error::IrreplaceableTransaction => FfiError::IrreplaceableTransaction,
|
||||
Error::FeeRateTooLow { .. } => FfiError::FeeRateTooLow,
|
||||
Error::FeeTooLow { .. } => FfiError::FeeTooLow,
|
||||
Error::MissingKeyOrigin(_) => FfiError::MissingKeyOrigin,
|
||||
Error::Key(_) => FfiError::Key,
|
||||
Error::ChecksumMismatch => FfiError::ChecksumMismatch,
|
||||
Error::SpendingPolicyRequired(_) => FfiError::SpendingPolicyRequired,
|
||||
Error::InvalidPolicyPathError(_) => FfiError::InvalidPolicyPathError,
|
||||
Error::Signer(_) => FfiError::Signer,
|
||||
Error::InvalidProgressValue(_) => FfiError::InvalidProgressValue,
|
||||
Error::ProgressUpdateError => FfiError::ProgressUpdateError,
|
||||
Error::InvalidOutpoint(_) => FfiError::InvalidOutpoint,
|
||||
Error::Descriptor(_) => FfiError::Descriptor,
|
||||
Error::AddressValidator(_) => FfiError::AddressValidator,
|
||||
Error::Encode(_) => FfiError::Encode,
|
||||
Error::Miniscript(_) => FfiError::Miniscript,
|
||||
Error::Bip32(_) => FfiError::Bip32,
|
||||
Error::Secp256k1(_) => FfiError::Secp256k1,
|
||||
Error::Json(_) => FfiError::Json,
|
||||
Error::Hex(_) => FfiError::Hex,
|
||||
Error::Psbt(_) => FfiError::Psbt,
|
||||
Error::Electrum(_) => FfiError::Electrum,
|
||||
// Error::Esplora(_) => JniError::Esplora,
|
||||
// Error::CompactFilters(_) => JniError::CompactFilters,
|
||||
Error::Sled(_) => FfiError::Sled,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
use crate::error::FfiError;
|
||||
use ::safer_ffi::prelude::*;
|
||||
use safer_ffi::char_p::char_p_boxed;
|
||||
|
||||
@ -6,14 +7,14 @@ use safer_ffi::char_p::char_p_boxed;
|
||||
#[derive(Debug)]
|
||||
pub struct FfiResult<T> {
|
||||
pub ok: T,
|
||||
pub err: char_p_boxed,
|
||||
pub err: FfiError,
|
||||
}
|
||||
|
||||
#[derive_ReprC]
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct FfiResultVoid {
|
||||
pub err: char_p_boxed,
|
||||
pub err: FfiError,
|
||||
}
|
||||
|
||||
#[ffi_export]
|
||||
|
@ -12,7 +12,7 @@ use safer_ffi::char_p::{char_p_boxed, char_p_ref};
|
||||
use blockchain::BlockchainConfig;
|
||||
use database::DatabaseConfig;
|
||||
|
||||
use crate::error::get_name;
|
||||
use crate::error::FfiError;
|
||||
use crate::types::{FfiResult, FfiResultVoid};
|
||||
use std::ffi::CString;
|
||||
|
||||
@ -43,11 +43,11 @@ fn new_wallet_result(
|
||||
match wallet_result {
|
||||
Ok(w) => FfiResult {
|
||||
ok: Some(Box::new(OpaqueWallet { raw: w })),
|
||||
err: char_p_boxed::from(CString::default()),
|
||||
err: FfiError::None,
|
||||
},
|
||||
Err(e) => FfiResult {
|
||||
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));
|
||||
match int_result {
|
||||
Ok(_v) => FfiResultVoid {
|
||||
err: char_p_boxed::from(CString::default()),
|
||||
err: FfiError::None,
|
||||
},
|
||||
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 {
|
||||
Ok(a) => FfiResult {
|
||||
ok: char_p_boxed::try_from(a).unwrap(),
|
||||
err: char_p_boxed::from(CString::default()),
|
||||
err: FfiError::None,
|
||||
},
|
||||
Err(e) => FfiResult {
|
||||
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();
|
||||
repr_c::Vec::from(ve)
|
||||
},
|
||||
err: char_p_boxed::from(CString::default()),
|
||||
err: FfiError::None,
|
||||
},
|
||||
Err(e) => FfiResult {
|
||||
ok: repr_c::Vec::EMPTY,
|
||||
err: char_p_boxed::try_from(get_name(&e)).unwrap(),
|
||||
err: FfiError::from(&e),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user