use std::{error::Error as Err, fmt};
#[derive(Debug)]
pub enum Error {
#[cfg(feature = "jwt")]
InvalidKeyFormat,
Base64Decode(data_encoding::DecodeError),
Http(http::Error),
HttpStatus(http::StatusCode),
Json(serde_json::Error),
Auth(AuthError),
#[cfg(feature = "jwt")]
InvalidRsaKey(ring::error::Unspecified),
#[cfg(feature = "jwt")]
InvalidRsaKeyRejected(ring::error::KeyRejected),
Poisoned,
#[cfg(feature = "gcp")]
Io(std::io::Error),
#[cfg(feature = "gcp")]
InvalidCredentials {
file: std::path::PathBuf,
error: Box<Error>,
},
SystemTime(std::time::SystemTimeError),
InvalidTokenFormat,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#![allow(clippy::enum_glob_use)]
use Error::*;
match self {
#[cfg(feature = "jwt")]
InvalidKeyFormat => f.write_str("The key format is invalid or unknown"),
Base64Decode(err) => write!(f, "{}", err),
Http(err) => write!(f, "{}", err),
HttpStatus(sc) => write!(f, "HTTP error status: {}", sc),
Json(err) => write!(f, "{}", err),
Auth(err) => write!(f, "{}", err),
#[cfg(feature = "jwt")]
InvalidRsaKey(_err) => f.write_str("RSA key is invalid"),
#[cfg(feature = "jwt")]
InvalidRsaKeyRejected(err) => write!(f, "RSA key is invalid: {}", err),
Poisoned => f.write_str("A mutex is poisoned"),
#[cfg(feature = "gcp")]
Io(inner) => write!(f, "{}", inner),
#[cfg(feature = "gcp")]
InvalidCredentials { file, error } => {
write!(f, "Invalid credentials in '{}': {}", file.display(), error)
}
SystemTime(te) => {
write!(f, "System Time error: {}", te)
}
InvalidTokenFormat => {
write!(f, "Invalid token format")
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn Err + 'static)> {
#![allow(clippy::enum_glob_use)]
use Error::*;
match self {
Base64Decode(err) => Some(err as &dyn Err),
Http(err) => Some(err as &dyn Err),
Json(err) => Some(err as &dyn Err),
Auth(err) => Some(err as &dyn Err),
SystemTime(err) => Some(err as &dyn Err),
_ => None,
}
}
}
impl From<data_encoding::DecodeError> for Error {
fn from(e: data_encoding::DecodeError) -> Self {
Error::Base64Decode(e)
}
}
impl From<http::Error> for Error {
fn from(e: http::Error) -> Self {
Error::Http(e)
}
}
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Json(e)
}
}
impl From<std::time::SystemTimeError> for Error {
fn from(e: std::time::SystemTimeError) -> Self {
Error::SystemTime(e)
}
}
#[derive(serde::Deserialize, Debug)]
pub struct AuthError {
pub error: Option<String>,
pub error_description: Option<String>,
}
impl fmt::Display for AuthError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref err) = self.error {
write!(f, "{}", err)?;
if let Some(ref desc) = self.error_description {
write!(f, "desc: {}", desc)?;
}
}
Ok(())
}
}
impl std::error::Error for AuthError {}