use thiserror::Error;
#[derive(Debug, Error)]
pub enum XurlError {
#[error("HTTP Error: {0}")]
Http(String),
#[error("IO Error: {0}")]
Io(String),
#[error("Invalid Method: Invalid HTTP method: {0}")]
InvalidMethod(String),
#[error("{0}")]
Api(String),
#[error("JSON Error: {0}")]
Json(String),
#[error("Auth Error: {0}")]
Auth(String),
#[error("Token Store Error: {0}")]
TokenStore(String),
}
#[allow(dead_code)] impl XurlError {
pub fn api(body: impl Into<String>) -> Self {
Self::Api(body.into())
}
pub fn auth(message: impl Into<String>) -> Self {
Self::Auth(message.into())
}
pub fn auth_with_cause(message: &str, cause: &dyn std::fmt::Display) -> Self {
Self::Auth(format!("{message} (cause: {cause})"))
}
pub fn token_store(message: impl Into<String>) -> Self {
Self::TokenStore(message.into())
}
#[must_use]
pub fn is_api(&self) -> bool {
matches!(self, Self::Api(_))
}
}
impl From<reqwest::Error> for XurlError {
fn from(err: reqwest::Error) -> Self {
Self::Http(err.to_string())
}
}
impl From<std::io::Error> for XurlError {
fn from(err: std::io::Error) -> Self {
Self::Io(err.to_string())
}
}
impl From<serde_json::Error> for XurlError {
fn from(err: serde_json::Error) -> Self {
Self::Json(err.to_string())
}
}
impl From<serde_yaml::Error> for XurlError {
fn from(err: serde_yaml::Error) -> Self {
Self::Json(err.to_string())
}
}
impl From<url::ParseError> for XurlError {
fn from(err: url::ParseError) -> Self {
Self::Http(err.to_string())
}
}
pub type Result<T> = std::result::Result<T, XurlError>;