use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Clone, Error, PartialEq)]
pub enum ClientError {
#[error("I/O error: {0:?}")]
IoError(String),
#[error("API error: {0}")]
ApiError(ApiErrorResponse),
#[error("Unexpected response code {0}, expected {1}")]
UnexpectedStatusCode(u16, u16),
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
pub struct ApiErrorResponse {
#[serde(rename = "error")]
pub message: String,
#[serde(rename = "status")]
pub status_code: u16,
}
impl std::fmt::Display for ApiErrorResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.status_code, self.message)
}
}
impl From<reqwest::Error> for ClientError {
fn from(err: reqwest::Error) -> Self {
ClientError::IoError(err.to_string())
}
}