1use crate::{EricApiPayload, ValidationReport};
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum EricError {
6 #[error("API Error (code={code}): {message}")]
7 ApiError {
8 code: i32,
9 message: String,
10 payload: EricApiPayload,
11 },
12 #[error("Internal error: {0}")]
14 Internal(#[from] anyhow::Error),
15}
16
17impl EricError {
18 pub fn code(&self) -> Option<i32> {
20 match self {
21 Self::ApiError { code, .. } => Some(*code),
22 Self::Internal(_) => None,
23 }
24 }
25
26 pub fn validation_response(&self) -> Option<&str> {
28 match self {
29 Self::ApiError { payload, .. } => Some(payload.validation_response.as_str()),
30 Self::Internal(_) => None,
31 }
32 }
33
34 pub fn server_response(&self) -> Option<&str> {
36 match self {
37 Self::ApiError { payload, .. } => Some(payload.server_response.as_str()),
38 Self::Internal(_) => None,
39 }
40 }
41
42 pub fn validation_report(&self) -> Result<Option<ValidationReport>, EricError> {
44 let Some(validation_response) = self.validation_response() else {
45 return Ok(None);
46 };
47
48 ValidationReport::parse(validation_response).map(Some)
49 }
50}