use std::{convert::From, fmt};
#[derive(Debug)]
#[non_exhaustive]
pub enum ErrorKind {
Http(reqwest::Error),
HttpStatus(reqwest::StatusCode),
Auth(AuthErrorDetails),
Metadata(String),
TonicMetadata(tonic::metadata::errors::InvalidMetadataValue),
Jwt(jsonwebtoken::errors::Error),
TokenSource,
CredentialsJson(serde_json::Error),
CredentialsFile(std::io::Error),
TokenJson(serde_json::Error),
TokenData,
GrpcStatus(tonic::transport::Error),
UrlError(hyper::http::uri::InvalidUri),
ExternalCredsSourceError(String),
HeaderValue(hyper::header::InvalidHeaderValue),
}
#[derive(Debug)]
pub struct AuthErrorDetails {
pub status: Option<reqwest::StatusCode>,
pub oauth_error: Option<String>,
pub details: Option<String>,
}
impl AuthErrorDetails {
pub fn hint(&self) -> Option<&'static str> {
match self.oauth_error.as_deref() {
Some("invalid_grant") => Some(
"the credentials are likely expired or revoked; re-authenticate (e.g. `gcloud auth application-default login`) or provide a new service account key",
),
_ => None,
}
}
}
impl fmt::Display for AuthErrorDetails {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut separate = false;
if let Some(ref status) = self.status {
write!(f, "HTTP {}", status)?;
separate = true;
}
if let Some(ref e) = self.oauth_error {
if separate {
write!(f, ", ")?;
}
write!(f, "oauth error: {}", e)?;
separate = true;
}
if let Some(ref d) = self.details {
if separate {
write!(f, " ")?;
}
write!(f, "({})", d)?;
}
if let Some(hint) = self.hint() {
write!(f, ". Hint: {}", hint)?;
}
Ok(())
}
}
#[derive(Debug)]
pub struct Error(Box<ErrorKind>);
impl Error {
pub fn kind(&self) -> &ErrorKind {
&self.0
}
pub fn into_kind(self) -> ErrorKind {
*self.0
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use ErrorKind::*;
match *self.0 {
Http(ref e) => write!(f, "http error: {}", e),
HttpStatus(ref s) => write!(f, "http status error: {}", s),
Auth(ref details) => write!(f, "authentication error: {}", details),
Metadata(ref e) => write!(f, "gce metadata service error: {}", e),
Jwt(ref e) => write!(f, "jwt error: {}", e),
TokenSource => write!(f, "token source error: not found token source"),
CredentialsJson(ref e) => write!(f, "credentials json error: {}", e),
CredentialsFile(ref e) => write!(f, "credentials file error: {}", e),
TokenJson(ref e) => write!(f, "token json error: {}", e),
TokenData => write!(f, "token data error: invalid token response data"),
GrpcStatus(ref e) => write!(f, "Tonic/gRPC error: {}", e),
TonicMetadata(ref e) => write!(f, "Tonic metadata error: {}", e),
UrlError(ref e) => write!(f, "Url error: {}", e),
ExternalCredsSourceError(ref e) => write!(f, "External creds source error: {}", e),
HeaderValue(ref e) => write!(f, "invalid header value: {}", e),
}
}
}
impl std::error::Error for Error {}
impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Self {
ErrorKind::Http(e).into()
}
}
impl From<jsonwebtoken::errors::Error> for Error {
fn from(e: jsonwebtoken::errors::Error) -> Self {
ErrorKind::Jwt(e).into()
}
}
impl From<ErrorKind> for Error {
fn from(k: ErrorKind) -> Self {
Error(Box::new(k))
}
}
impl From<tonic::transport::Error> for Error {
fn from(e: tonic::transport::Error) -> Self {
ErrorKind::GrpcStatus(e).into()
}
}
impl From<tonic::metadata::errors::InvalidMetadataValue> for Error {
fn from(e: tonic::metadata::errors::InvalidMetadataValue) -> Self {
ErrorKind::TonicMetadata(e).into()
}
}
impl From<hyper::http::uri::InvalidUri> for Error {
fn from(e: hyper::http::uri::InvalidUri) -> Self {
ErrorKind::UrlError(e).into()
}
}
impl From<hyper::header::InvalidHeaderValue> for Error {
fn from(e: hyper::header::InvalidHeaderValue) -> Self {
ErrorKind::HeaderValue(e).into()
}
}
pub type Result<T> = std::result::Result<T, Error>;