noxious_client/
error.rs

1use serde::{Deserialize, Serialize};
2use thiserror::Error;
3
4/// The errors that the client returns
5#[derive(Debug, Clone, Error, PartialEq)]
6pub enum ClientError {
7    /// An I/O error happened
8    #[error("I/O error: {0:?}")]
9    IoError(String),
10    /// Server returned an error
11    #[error("API error: {0}")]
12    ApiError(ApiErrorResponse),
13    /// Unexpected status code, cannot parse the response body
14    #[error("Unexpected response code {0}, expected {1}")]
15    UnexpectedStatusCode(u16, u16),
16}
17
18/// The struct that describes the error response
19#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
20pub struct ApiErrorResponse {
21    /// The error message
22    #[serde(rename = "error")]
23    pub message: String,
24    /// The error code which is usually the same as the http status code
25    #[serde(rename = "status")]
26    pub status_code: u16,
27}
28
29impl std::fmt::Display for ApiErrorResponse {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        write!(f, "{}: {}", self.status_code, self.message)
32    }
33}
34
35impl From<reqwest::Error> for ClientError {
36    fn from(err: reqwest::Error) -> Self {
37        ClientError::IoError(err.to_string())
38    }
39}