2026-01-21 15:22:38 +01:00
|
|
|
//! Error handling for the FFI layer.
|
|
|
|
|
//!
|
2026-01-26 10:29:37 +01:00
|
|
|
//! Uses numeric error codes with error messages printed to stderr.
|
2026-01-21 15:22:38 +01:00
|
|
|
|
|
|
|
|
/// Error codes returned by FFI functions.
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
|
|
|
pub enum WalletFfiError {
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Operation completed successfully.
|
2026-01-21 15:22:38 +01:00
|
|
|
Success = 0,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// A null pointer was passed where a valid pointer was expected.
|
2026-01-21 15:22:38 +01:00
|
|
|
NullPointer = 1,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Invalid UTF-8 string.
|
2026-01-21 15:22:38 +01:00
|
|
|
InvalidUtf8 = 2,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Wallet handle is not initialized.
|
2026-01-21 15:22:38 +01:00
|
|
|
WalletNotInitialized = 3,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Configuration error.
|
2026-01-21 15:22:38 +01:00
|
|
|
ConfigError = 4,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Storage/persistence error.
|
2026-01-21 15:22:38 +01:00
|
|
|
StorageError = 5,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Network/RPC error.
|
2026-01-21 15:22:38 +01:00
|
|
|
NetworkError = 6,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Account not found.
|
2026-01-21 15:22:38 +01:00
|
|
|
AccountNotFound = 7,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Key not found for account.
|
2026-01-21 15:22:38 +01:00
|
|
|
KeyNotFound = 8,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Insufficient funds for operation.
|
2026-01-21 15:22:38 +01:00
|
|
|
InsufficientFunds = 9,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Invalid account ID format.
|
2026-01-21 15:22:38 +01:00
|
|
|
InvalidAccountId = 10,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Tokio runtime error.
|
2026-01-21 15:22:38 +01:00
|
|
|
RuntimeError = 11,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Password required but not provided.
|
2026-01-21 15:22:38 +01:00
|
|
|
PasswordRequired = 12,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Block synchronization error.
|
2026-01-21 15:22:38 +01:00
|
|
|
SyncError = 13,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Serialization/deserialization error.
|
2026-01-21 15:22:38 +01:00
|
|
|
SerializationError = 14,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Invalid conversion from FFI types to NSSA types.
|
2026-02-05 12:25:21 -03:00
|
|
|
InvalidTypeConversion = 15,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Invalid Key value.
|
2026-02-05 12:25:21 -03:00
|
|
|
InvalidKeyValue = 16,
|
2026-03-10 00:17:43 +03:00
|
|
|
/// Internal error (catch-all).
|
2026-01-21 15:22:38 +01:00
|
|
|
InternalError = 99,
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-03 23:21:08 +03:00
|
|
|
impl WalletFfiError {
|
|
|
|
|
/// Check if it's [`WalletFfiError::Success`] or panic.
|
|
|
|
|
pub fn unwrap(self) {
|
2026-03-09 18:27:56 +03:00
|
|
|
let Self::Success = self else {
|
2026-03-03 23:21:08 +03:00
|
|
|
panic!("Called `unwrap()` on error value `{self:#?}`");
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 10:29:37 +01:00
|
|
|
/// Log an error message to stderr.
|
2026-03-04 18:42:33 +03:00
|
|
|
#[expect(
|
|
|
|
|
clippy::print_stderr,
|
|
|
|
|
reason = "In FFI context it's better to print errors than to return strings"
|
|
|
|
|
)]
|
2026-01-26 10:29:37 +01:00
|
|
|
pub fn print_error(msg: impl Into<String>) {
|
|
|
|
|
eprintln!("[wallet-ffi] {}", msg.into());
|
2026-01-21 15:22:38 +01:00
|
|
|
}
|