Skip to main content

jx3_api/
err.rs

1use std::error::Error;
2use std::fmt::Display;
3
4#[derive(Debug)]
5pub enum Jx3ApiError {
6    HttpError(reqwest::Error),
7    JsonError(serde_json::Error),
8    ApiError(i32, String),
9}
10
11impl Display for Jx3ApiError {
12    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
13        match self {
14            Jx3ApiError::HttpError(e) => write!(f, "HTTP error: {}", e),
15            Jx3ApiError::JsonError(e) => write!(f, "JSON parsing error: {}", e),
16            Jx3ApiError::ApiError(code, msg) => {
17                write!(f, "API request failed: code={}, message={}", code, msg)
18            }
19        }
20    }
21}
22
23impl Error for Jx3ApiError {
24    fn source(&self) -> Option<&(dyn Error + 'static)> {
25        match self {
26            Jx3ApiError::HttpError(e) => Some(e),
27            Jx3ApiError::JsonError(e) => Some(e),
28            Jx3ApiError::ApiError(_code, _msg) => None,
29        }
30    }
31}
32
33impl From<reqwest::Error> for Jx3ApiError {
34    fn from(error: reqwest::Error) -> Self {
35        Jx3ApiError::HttpError(error)
36    }
37}
38
39impl From<serde_json::Error> for Jx3ApiError {
40    fn from(error: serde_json::Error) -> Self {
41        Jx3ApiError::JsonError(error)
42    }
43}