1use cdk::Error as CdkError;
4use cdk_common::error::ErrorResponse;
5
6#[derive(Debug, thiserror::Error, uniffi::Error)]
12pub enum FfiError {
13 #[error("[{code}] {error_message}")]
16 Cdk {
17 code: u32,
19 error_message: String,
21 },
22
23 #[error("{error_message}")]
26 Internal {
27 error_message: String,
29 },
30}
31
32impl FfiError {
33 pub fn internal(msg: impl ToString) -> Self {
35 FfiError::Internal {
36 error_message: msg.to_string(),
37 }
38 }
39
40 pub fn database(msg: impl ToString) -> Self {
42 FfiError::Cdk {
43 code: 50000,
44 error_message: msg.to_string(),
45 }
46 }
47}
48
49impl From<CdkError> for FfiError {
50 fn from(err: CdkError) -> Self {
51 let response = ErrorResponse::from(err);
52 FfiError::Cdk {
53 code: response.code.to_code() as u32,
54 error_message: response.detail,
55 }
56 }
57}
58
59impl From<cdk::amount::Error> for FfiError {
60 fn from(err: cdk::amount::Error) -> Self {
61 FfiError::internal(err)
62 }
63}
64
65impl From<cdk::nuts::nut00::Error> for FfiError {
66 fn from(err: cdk::nuts::nut00::Error) -> Self {
67 FfiError::internal(err)
68 }
69}
70
71impl From<serde_json::Error> for FfiError {
72 fn from(err: serde_json::Error) -> Self {
73 FfiError::internal(err)
74 }
75}