use crate::{EricApiPayload, ValidationReport};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum EricError {
#[error("API Error (code={code}): {message}")]
ApiError {
code: i32,
message: String,
payload: EricApiPayload,
},
#[error("Internal error: {0}")]
Internal(#[from] anyhow::Error),
}
impl EricError {
pub fn code(&self) -> Option<i32> {
match self {
Self::ApiError { code, .. } => Some(*code),
Self::Internal(_) => None,
}
}
pub fn validation_response(&self) -> Option<&str> {
match self {
Self::ApiError { payload, .. } => Some(payload.validation_response.as_str()),
Self::Internal(_) => None,
}
}
pub fn server_response(&self) -> Option<&str> {
match self {
Self::ApiError { payload, .. } => Some(payload.server_response.as_str()),
Self::Internal(_) => None,
}
}
pub fn validation_report(&self) -> Result<Option<ValidationReport>, EricError> {
let Some(validation_response) = self.validation_response() else {
return Ok(None);
};
ValidationReport::parse(validation_response).map(Some)
}
}