gcloud_sdk/
error.rs

1use std::{convert::From, fmt};
2
3/// Represents the details of the [`Error`](struct.Error.html)
4#[derive(Debug)]
5pub enum ErrorKind {
6    /// Errors that can possibly occur while accessing an HTTP server.
7    Http(reqwest::Error),
8    /// Http status code that is not 2xx when getting token.
9    HttpStatus(reqwest::StatusCode),
10    /// GCE metadata service error.
11    Metadata(String),
12    TonicMetadata(tonic::metadata::errors::InvalidMetadataValue),
13    /// JWT encode/decode error.
14    Jwt(jsonwebtoken::errors::Error),
15    /// Token source error.
16    TokenSource,
17    /// An error parsing credentials file.
18    CredentialsJson(serde_json::Error),
19    /// An error reading credentials file.
20    CredentialsFile(std::io::Error),
21    /// An error from json serialization and deserialization.
22    TokenJson(serde_json::Error),
23    /// Invalid token error.
24    TokenData,
25    GrpcStatus(tonic::transport::Error),
26    UrlError(hyper::http::uri::InvalidUri),
27    ExternalCredsSourceError(String),
28    #[doc(hidden)]
29    __Nonexhaustive,
30}
31
32/// Represents errors that can occur during getting token.
33#[derive(Debug)]
34pub struct Error(Box<ErrorKind>);
35
36impl Error {
37    /// Borrow [`ErrorKind`](enum.ErrorKind.html).
38    pub fn kind(&self) -> &ErrorKind {
39        &self.0
40    }
41
42    /// To own [`ErrorKind`](enum.ErrorKind.html).
43    pub fn into_kind(self) -> ErrorKind {
44        *self.0
45    }
46}
47
48impl fmt::Display for Error {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        use ErrorKind::*;
51        match *self.0 {
52            Http(ref e) => write!(f, "http error: {}", e),
53            HttpStatus(ref s) => write!(f, "http status error: {}", s),
54            Metadata(ref e) => write!(f, "gce metadata service error: {}", e),
55            Jwt(ref e) => write!(f, "jwt error: {}", e),
56            TokenSource => write!(f, "token source error: not found token source"),
57            CredentialsJson(ref e) => write!(f, "credentials json error: {}", e),
58            CredentialsFile(ref e) => write!(f, "credentials file error: {}", e),
59            TokenJson(ref e) => write!(f, "token json error: {}", e),
60            TokenData => write!(f, "token data error: invalid token response data"),
61            GrpcStatus(ref e) => write!(f, "Tonic/gRPC error: {}", e),
62            TonicMetadata(ref e) => write!(f, "Tonic metadata error: {}", e),
63            UrlError(ref e) => write!(f, "Url error: {}", e),
64            ExternalCredsSourceError(ref e) => write!(f, "External creds source error: {}", e),
65            __Nonexhaustive => write!(f, "unknown error"),
66        }
67    }
68}
69
70impl std::error::Error for Error {}
71
72impl From<reqwest::Error> for Error {
73    fn from(e: reqwest::Error) -> Self {
74        ErrorKind::Http(e).into()
75    }
76}
77
78impl From<jsonwebtoken::errors::Error> for Error {
79    fn from(e: jsonwebtoken::errors::Error) -> Self {
80        ErrorKind::Jwt(e).into()
81    }
82}
83
84impl From<ErrorKind> for Error {
85    fn from(k: ErrorKind) -> Self {
86        Error(Box::new(k))
87    }
88}
89
90impl From<tonic::transport::Error> for Error {
91    fn from(e: tonic::transport::Error) -> Self {
92        ErrorKind::GrpcStatus(e).into()
93    }
94}
95
96impl From<tonic::metadata::errors::InvalidMetadataValue> for Error {
97    fn from(e: tonic::metadata::errors::InvalidMetadataValue) -> Self {
98        ErrorKind::TonicMetadata(e).into()
99    }
100}
101
102impl From<hyper::http::uri::InvalidUri> for Error {
103    fn from(e: hyper::http::uri::InvalidUri) -> Self {
104        ErrorKind::UrlError(e).into()
105    }
106}
107
108/// Wrapper for the `Result` type with an [`Error`](struct.Error.html).
109pub type Result<T> = std::result::Result<T, Error>;