Skip to main content

tame_gcs/
error.rs

1//! Error facilities
2
3use std::fmt;
4
5/// Core error type for all errors possible from tame-gcs
6#[derive(thiserror::Error, Debug, PartialEq)]
7pub enum Error {
8    #[error("Expected {min}-{max} characters, found {len}")]
9    InvalidCharacterCount { len: usize, min: usize, max: usize },
10    #[error("Expected {min}-{max} bytes, found {len}")]
11    InvalidLength { len: usize, min: usize, max: usize },
12    #[error("Character '{1}' @ {0} is not allowed")]
13    InvalidCharacter(usize, char),
14    #[error("Prefix {0} is not allowed")]
15    InvalidPrefix(&'static str),
16    #[error("Sequence {0} is not allowed")]
17    InvalidSequence(&'static str),
18    #[error("Failed to parse URI")]
19    InvalidUri(UriError),
20    #[error("Header value is invalid")]
21    InvalidHeaderValue,
22    #[error("HTTP error")]
23    Http(#[source] HttpError),
24    #[error("HTTP status")]
25    HttpStatus(#[source] HttpStatusError),
26    #[error("An HTTP response didn't have a valid {0}")]
27    UnknownHeader(http::header::HeaderName),
28    #[error("GCS API error")]
29    Api(#[source] ApiError),
30    #[error("JSON error")]
31    Json(#[source] JsonError),
32    #[error("Response body doesn't contain enough data")]
33    InsufficientData,
34    #[error("Key rejected: {0}")]
35    KeyRejected(String),
36    #[error("An error occurred during signing")]
37    Signing,
38    #[error("An expiration duration was too long: requested = {requested}, max = {max}")]
39    TooLongExpiration { requested: u64, max: u64 },
40    #[error("Failed to parse url")]
41    UrlParse(#[source] url::ParseError),
42    #[error("Unable to stringize or parse header value '{0:?}'")]
43    OpaqueHeaderValue(http::header::HeaderValue),
44    #[error("I/O error occurred")]
45    Io(#[source] IoError),
46    #[error("Unable to decode base64")]
47    Base64Decode(#[source] data_encoding::DecodeError),
48    #[error("Unable to encode url")]
49    UrlEncode(#[source] serde_urlencoded::ser::Error),
50}
51
52#[derive(Debug, thiserror::Error)]
53pub struct HttpError(#[source] pub http::Error);
54
55impl fmt::Display for HttpError {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        write!(f, "{}", self.0)
58    }
59}
60
61impl PartialEq for HttpError {
62    fn eq(&self, _other: &Self) -> bool {
63        // I feel really bad about this
64        true
65    }
66}
67
68impl From<http::Error> for Error {
69    fn from(e: http::Error) -> Self {
70        Self::Http(HttpError(e))
71    }
72}
73
74#[derive(Debug, thiserror::Error)]
75pub struct HttpStatusError(pub http::StatusCode);
76
77impl PartialEq for HttpStatusError {
78    fn eq(&self, other: &Self) -> bool {
79        self.0 == other.0
80    }
81}
82
83impl fmt::Display for HttpStatusError {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        write!(f, "{}", self.0)
86    }
87}
88
89impl From<http::StatusCode> for Error {
90    fn from(e: http::StatusCode) -> Self {
91        Self::HttpStatus(HttpStatusError(e))
92    }
93}
94
95impl From<http::header::InvalidHeaderValue> for Error {
96    fn from(_: http::header::InvalidHeaderValue) -> Self {
97        Self::InvalidHeaderValue
98    }
99}
100
101#[derive(Debug, thiserror::Error)]
102pub struct IoError(#[source] pub std::io::Error);
103
104impl PartialEq for IoError {
105    fn eq(&self, other: &Self) -> bool {
106        self.0.kind() == other.0.kind()
107    }
108}
109
110impl fmt::Display for IoError {
111    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112        write!(f, "{}", self.0)
113    }
114}
115
116impl From<std::io::Error> for Error {
117    fn from(e: std::io::Error) -> Self {
118        Self::Io(IoError(e))
119    }
120}
121
122#[derive(Debug, thiserror::Error)]
123pub struct JsonError(#[source] pub serde_json::Error);
124
125impl fmt::Display for JsonError {
126    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127        write!(f, "{}", self.0)
128    }
129}
130
131impl PartialEq for JsonError {
132    fn eq(&self, other: &Self) -> bool {
133        self.0.classify() == other.0.classify()
134    }
135}
136
137impl From<serde_json::Error> for Error {
138    fn from(e: serde_json::Error) -> Self {
139        Self::Json(JsonError(e))
140    }
141}
142
143impl From<serde_urlencoded::ser::Error> for Error {
144    fn from(e: serde_urlencoded::ser::Error) -> Self {
145        Self::UrlEncode(e)
146    }
147}
148
149#[derive(Debug, thiserror::Error)]
150pub struct UriError(#[source] http::uri::InvalidUri);
151
152impl fmt::Display for UriError {
153    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154        write!(f, "{}", self.0)
155    }
156}
157
158impl PartialEq for UriError {
159    fn eq(&self, other: &Self) -> bool {
160        // This is **TERRIBLE** but all of the error details are unnecessarily
161        // private and it doesn't implement PartialEq ARGH
162        self.0.to_string() == other.0.to_string()
163    }
164}
165
166impl From<http::uri::InvalidUri> for Error {
167    fn from(e: http::uri::InvalidUri) -> Self {
168        Self::InvalidUri(UriError(e))
169    }
170}
171
172#[derive(Debug, PartialEq, Deserialize)]
173pub struct ApiErrorInner {
174    pub domain: Option<String>,
175    pub reason: Option<String>,
176    pub message: Option<String>,
177}
178
179#[derive(Debug, thiserror::Error, PartialEq, Deserialize)]
180pub struct ApiError {
181    pub code: u16,
182    pub message: String,
183    pub errors: Vec<ApiErrorInner>,
184}
185
186impl fmt::Display for ApiError {
187    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
188        write!(f, "{:#?}", self)
189    }
190}
191
192#[cfg(feature = "signing")]
193impl From<ring::error::KeyRejected> for Error {
194    fn from(re: ring::error::KeyRejected) -> Self {
195        Self::KeyRejected(format!("{}", re))
196    }
197}
198
199#[cfg(feature = "signing")]
200impl From<ring::error::Unspecified> for Error {
201    fn from(_re: ring::error::Unspecified) -> Self {
202        Self::Signing
203    }
204}