1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! FFI Error types
use cdk::Error as CdkError;
use cdk_common::error::ErrorResponse;
/// FFI Error type that wraps CDK errors for cross-language use
///
/// This simplified error type uses protocol-compliant error codes from `ErrorCode`
/// in `cdk-common`, reducing duplication while providing structured error information
/// to FFI consumers.
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum FfiError {
/// CDK error with protocol-compliant error code
/// The code corresponds to the Cashu protocol error codes (e.g., 11001, 20001, etc.)
#[error("[{code}] {error_message}")]
Cdk {
/// Error code from the Cashu protocol specification
code: u32,
/// Human-readable error message
error_message: String,
},
/// Internal/infrastructure error (no protocol error code)
/// Used for errors that don't map to Cashu protocol codes
#[error("{error_message}")]
Internal {
/// Human-readable error message
error_message: String,
},
}
impl FfiError {
/// Create an internal error from any type that implements ToString
pub fn internal(msg: impl ToString) -> Self {
Self::Internal {
error_message: msg.to_string(),
}
}
/// Create a database error (uses Unknown code 50000)
pub fn database(msg: impl ToString) -> Self {
Self::Cdk {
code: 50000,
error_message: msg.to_string(),
}
}
}
impl From<CdkError> for FfiError {
fn from(err: CdkError) -> Self {
let response = ErrorResponse::from(err);
Self::Cdk {
code: response.code.to_code() as u32,
error_message: response.detail,
}
}
}
impl From<cdk::amount::Error> for FfiError {
fn from(err: cdk::amount::Error) -> Self {
FfiError::internal(err)
}
}
impl From<cdk::nuts::nut00::Error> for FfiError {
fn from(err: cdk::nuts::nut00::Error) -> Self {
FfiError::internal(err)
}
}
impl From<serde_json::Error> for FfiError {
fn from(err: serde_json::Error) -> Self {
FfiError::internal(err)
}
}