use cdk::Error as CdkError;
use cdk_common::error::ErrorResponse;
#[derive(Debug, thiserror::Error, uniffi::Error)]
pub enum FfiError {
#[error("[{code}] {error_message}")]
Cdk {
code: u32,
error_message: String,
},
#[error("{error_message}")]
Internal {
error_message: String,
},
}
impl FfiError {
pub fn internal(msg: impl ToString) -> Self {
Self::Internal {
error_message: msg.to_string(),
}
}
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)
}
}