use std::fmt::Display;
use thiserror::Error;
use serde::Deserialize;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("request failed")]
ReqwestErr(#[from] reqwest::Error),
#[error("authentification failed")]
AuthErr(AuthenticationError),
#[error("your don't have the right permission")]
PermErr(AuthorizationError),
#[error("can't decode request")]
DecodeErr(reqwest::Error),
#[error("can't parse url")]
ParseUrlErr(url::ParseError),
}
#[derive(Error, Debug, Deserialize)]
pub struct AuthenticationError {
pub error: String,
pub error_description: String,
}
impl Display for AuthenticationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.error, self.error_description)
}
}
#[derive(Error, Debug, Deserialize)]
pub struct AuthorizationError {
pub message: String,
}
impl Display for AuthorizationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "authorization error: {}", self.message)
}
}
#[cfg(test)]
mod tests {
use crate::Error;
use std::error::Error as StdError;
fn error_trait_implemented<T>()
where
T: StdError,
{
}
#[test]
pub fn error_trait() {
error_trait_implemented::<Error>();
}
}