Skip to main content

eric_sdk/
error.rs

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    /// Unstructured catch all for internal errors
13    #[error("Internal error: {0}")]
14    Internal(#[from] anyhow::Error),
15}
16
17impl EricError {
18    /// Returns the ERiC error code if this is an API error.
19    pub fn code(&self) -> Option<i32> {
20        match self {
21            Self::ApiError { code, .. } => Some(*code),
22            Self::Internal(_) => None,
23        }
24    }
25
26    /// Returns the raw validation XML returned by ERiC.
27    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    /// Returns the raw server XML returned by ERiC.
35    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    /// Parses the validation XML into a structured report.
43    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}