Skip to main content

native/kameleoon_elixir_bridge/src/error_ex.rs

use kameleoon_core::error::KameleoonError;
use rustler::Error as NifError;

#[derive(Debug, rustler::NifException)]
#[module = "Kameleoon.Error"]
#[rustler(encode)]
pub(crate) struct ErrorEx {
    code: String,
    message: String,
}

impl From<&KameleoonError> for ErrorEx {
    fn from(value: &KameleoonError) -> Self {
        Self {
            code: format!("{:?}", value.code()),
            message: value.message().to_owned(),
        }
    }
}

pub(crate) fn to_nif_error(err: KameleoonError) -> NifError {
    NifError::Term(Box::new(ErrorEx::from(&err)))
}