use super::http::HttpError;
use serde_json::Error;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum ClientError {
#[error("client id generation failed")]
ClientIDGenerationFailed,
#[error("invalid id")]
InvalidId,
#[error("not found")]
NotFound,
#[error("invalid url")]
InvalidUrl,
#[error("http error: {0}")]
Http(Box<HttpError>),
#[error("json parse error: {0}")]
ParseJson(#[from] serde_json::Error),
#[error("json parse error at `{field_path}`: {error}")]
ParseJsonWithContext {
field_path: String,
error: String,
},
#[error("url parse error: {0}")]
ParseUrl(#[from] url::ParseError),
#[error("input/output error: {0}")]
Io(#[from] std::io::Error),
#[error("authentication required using oauth token")]
ShouldBeAuthenticated(),
#[error("custom error: {0}")]
Custom(String),
#[error("unknown error")]
Unknown,
}
impl From<HttpError> for ClientError {
fn from(err: HttpError) -> Self {
Self::Http(Box::new(err))
}
}
pub fn convert_serde_path_to_error(e: serde_path_to_error::Error<Error>) -> ClientError {
ClientError::ParseJsonWithContext {
error: e.to_string(),
field_path: e.path().to_string(),
}
}
pub fn convert_404_to_invalid_id(error: ClientError) -> ClientError {
if let ClientError::Http(ref http_err) = error {
if let HttpError::StatusCode(response) = http_err.as_ref() {
if response.status() == reqwest::StatusCode::NOT_FOUND {
return ClientError::InvalidId;
}
}
}
error
}