64 lines
1.8 KiB
Rust
Raw Normal View History

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 {
/// Operation completed successfully
Success = 0,
/// A null pointer was passed where a valid pointer was expected
NullPointer = 1,
/// Invalid UTF-8 string
InvalidUtf8 = 2,
/// Wallet handle is not initialized
WalletNotInitialized = 3,
/// Configuration error
ConfigError = 4,
/// Storage/persistence error
StorageError = 5,
/// Network/RPC error
NetworkError = 6,
/// Account not found
AccountNotFound = 7,
/// Key not found for account
KeyNotFound = 8,
/// Insufficient funds for operation
InsufficientFunds = 9,
/// Invalid account ID format
InvalidAccountId = 10,
/// Tokio runtime error
RuntimeError = 11,
/// Password required but not provided
PasswordRequired = 12,
/// Block synchronization error
SyncError = 13,
/// Serialization/deserialization error
SerializationError = 14,
2026-02-04 12:11:43 -03:00
/// Invalid conversion from FFI types to NSSA types
InvalidTypeConversion = 15,
2026-02-04 15:27:39 -03:00
/// Invalid Key value
InvalidKeyValue = 16,
2026-01-21 15:22:38 +01:00
/// Internal error (catch-all)
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
}