google_cloud_auth/
build_errors.rs1type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
18
19#[derive(thiserror::Error, Debug)]
27#[error(transparent)]
28pub struct Error(ErrorKind);
29
30impl Error {
31 pub fn is_loading(&self) -> bool {
33 matches!(self.0, ErrorKind::Loading(_))
34 }
35
36 pub fn is_parsing(&self) -> bool {
38 matches!(self.0, ErrorKind::Parsing(_))
39 }
40
41 pub fn is_unknown_type(&self) -> bool {
43 matches!(self.0, ErrorKind::UnknownType(_))
44 }
45
46 pub fn is_missing_field(&self) -> bool {
48 matches!(self.0, ErrorKind::MissingField(_))
49 }
50
51 #[cfg(feature = "idtoken")]
52 pub fn is_not_supported(&self) -> bool {
54 matches!(self.0, ErrorKind::NotSupported(_))
55 }
56
57 pub(crate) fn loading<T>(source: T) -> Error
60 where
61 T: Into<BoxError>,
62 {
63 Error(ErrorKind::Loading(source.into()))
64 }
65
66 pub(crate) fn parsing<T>(source: T) -> Error
68 where
69 T: Into<BoxError>,
70 {
71 Error(ErrorKind::Parsing(source.into()))
72 }
73
74 pub(crate) fn unknown_type<T>(source: T) -> Error
76 where
77 T: Into<BoxError>,
78 {
79 Error(ErrorKind::UnknownType(source.into()))
80 }
81
82 pub(crate) fn missing_field(field: &'static str) -> Error {
84 Error(ErrorKind::MissingField(field))
85 }
86
87 #[cfg(feature = "idtoken")]
88 pub(crate) fn not_supported<T>(credential_type: T) -> Error
90 where
91 T: Into<BoxError>,
92 {
93 Error(ErrorKind::NotSupported(credential_type.into()))
94 }
95}
96
97#[derive(thiserror::Error, Debug)]
98enum ErrorKind {
99 #[error("could not find or open the credentials file {0}")]
100 Loading(#[source] BoxError),
101 #[error("cannot parse the credentials file {0}")]
102 Parsing(#[source] BoxError),
103 #[error("unknown or invalid credentials type {0}")]
104 UnknownType(#[source] BoxError),
105 #[error("missing required field: {0}")]
106 MissingField(&'static str),
107 #[cfg(feature = "idtoken")]
108 #[error("credentials type not supported: {0}")]
109 NotSupported(#[source] BoxError),
110}
111
112#[cfg(test)]
113mod tests {
114 use super::*;
115 use std::error::Error as _;
116
117 #[test]
118 fn constructors() {
119 let error = Error::loading("test message");
120 assert!(error.is_loading(), "{error:?}");
121 assert!(error.source().is_some(), "{error:?}");
122 assert!(error.to_string().contains("test message"), "{error}");
123
124 let error = Error::parsing("test message");
125 assert!(error.is_parsing(), "{error:?}");
126 assert!(error.source().is_some(), "{error:?}");
127 assert!(error.to_string().contains("test message"), "{error}");
128
129 let error = Error::unknown_type("test message");
130 assert!(error.is_unknown_type(), "{error:?}");
131 assert!(error.source().is_some(), "{error:?}");
132 assert!(error.to_string().contains("test message"), "{error}");
133
134 let error = Error::missing_field("test field");
135 assert!(error.is_missing_field(), "{error:?}");
136 assert!(error.source().is_none(), "{error:?}");
137 assert!(error.to_string().contains("test field"), "{error}");
138 }
139}