cdk_ffi/
error.rs

1//! FFI Error types
2
3use cdk::Error as CdkError;
4
5/// FFI Error type that wraps CDK errors for cross-language use
6#[derive(Debug, thiserror::Error, uniffi::Error)]
7#[uniffi(flat_error)]
8pub enum FfiError {
9    /// Generic error with message
10    #[error("CDK Error: {msg}")]
11    Generic { msg: String },
12
13    /// Amount overflow
14    #[error("Amount overflow")]
15    AmountOverflow,
16
17    /// Division by zero
18    #[error("Division by zero")]
19    DivisionByZero,
20
21    /// Amount error
22    #[error("Amount error: {msg}")]
23    Amount { msg: String },
24
25    /// Payment failed
26    #[error("Payment failed")]
27    PaymentFailed,
28
29    /// Payment pending
30    #[error("Payment pending")]
31    PaymentPending,
32
33    /// Insufficient funds
34    #[error("Insufficient funds")]
35    InsufficientFunds,
36
37    /// Database error
38    #[error("Database error: {msg}")]
39    Database { msg: String },
40
41    /// Network error
42    #[error("Network error: {msg}")]
43    Network { msg: String },
44
45    /// Invalid token
46    #[error("Invalid token: {msg}")]
47    InvalidToken { msg: String },
48
49    /// Wallet error
50    #[error("Wallet error: {msg}")]
51    Wallet { msg: String },
52
53    /// Keyset unknown
54    #[error("Keyset unknown")]
55    KeysetUnknown,
56
57    /// Unit not supported
58    #[error("Unit not supported")]
59    UnitNotSupported,
60
61    /// Runtime task join error
62    #[error("Runtime task join error: {msg}")]
63    RuntimeTaskJoin { msg: String },
64
65    /// Invalid mnemonic phrase
66    #[error("Invalid mnemonic: {msg}")]
67    InvalidMnemonic { msg: String },
68
69    /// URL parsing error
70    #[error("Invalid URL: {msg}")]
71    InvalidUrl { msg: String },
72
73    /// Hex format error
74    #[error("Invalid hex format: {msg}")]
75    InvalidHex { msg: String },
76
77    /// Cryptographic key parsing error
78    #[error("Invalid cryptographic key: {msg}")]
79    InvalidCryptographicKey { msg: String },
80
81    /// Serialization/deserialization error
82    #[error("Serialization error: {msg}")]
83    Serialization { msg: String },
84}
85
86impl From<CdkError> for FfiError {
87    fn from(err: CdkError) -> Self {
88        match err {
89            CdkError::AmountOverflow => FfiError::AmountOverflow,
90            CdkError::PaymentFailed => FfiError::PaymentFailed,
91            CdkError::PaymentPending => FfiError::PaymentPending,
92            CdkError::InsufficientFunds => FfiError::InsufficientFunds,
93            CdkError::UnsupportedUnit => FfiError::UnitNotSupported,
94            CdkError::KeysetUnknown(_) => FfiError::KeysetUnknown,
95            _ => FfiError::Generic {
96                msg: err.to_string(),
97            },
98        }
99    }
100}
101
102impl From<cdk::amount::Error> for FfiError {
103    fn from(err: cdk::amount::Error) -> Self {
104        FfiError::Amount {
105            msg: err.to_string(),
106        }
107    }
108}
109
110impl From<cdk::nuts::nut00::Error> for FfiError {
111    fn from(err: cdk::nuts::nut00::Error) -> Self {
112        FfiError::Generic {
113            msg: err.to_string(),
114        }
115    }
116}
117
118impl From<serde_json::Error> for FfiError {
119    fn from(err: serde_json::Error) -> Self {
120        FfiError::Serialization {
121            msg: err.to_string(),
122        }
123    }
124}