1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use std::{convert::From, fmt};
#[derive(Debug)]
pub enum ErrorKind {
Http(attohttpc::Error),
HttpStatus(attohttpc::StatusCode),
Metadata(gcemeta::Error),
Jwt(jsonwebtoken::errors::Error),
TokenSource,
CredentialsJson(serde_json::Error),
CredentialsFile(std::io::Error),
TokenJson(serde_json::Error),
TokenData,
#[doc(hidden)]
__Nonexhaustive,
}
#[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),
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"),
__Nonexhaustive => write!(f, "unknown error"),
}
}
}
impl std::error::Error for Error {}
impl From<attohttpc::Error> for Error {
fn from(e: attohttpc::Error) -> Self {
ErrorKind::Http(e).into()
}
}
impl From<gcemeta::Error> for Error {
fn from(e: gcemeta::Error) -> Self {
ErrorKind::Metadata(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))
}
}
pub type Result<T> = std::result::Result<T, Error>;