cloud_storage/error.rs
1/// Represents any of the ways storing something in Google Cloud Storage can fail.
2#[derive(Debug)]
3pub enum Error {
4 /// If the error is caused by a non 2xx response by Google, this variant is returned.
5 Google(GoogleErrorResponse),
6 /// If another network error causes something to fail, this variant is used.
7 Reqwest(reqwest::Error),
8 /// If we encounter a problem decoding the private key, this variant is used.
9 #[cfg(feature = "ring")]
10 Pem(pem::PemError),
11 /// If we encounter a problem parsing the private key, this variant is used.
12 #[cfg(feature = "ring")]
13 KeyRejected(ring::error::KeyRejected),
14 /// If we encounter a problem signing a request, this variant is used.
15 #[cfg(feature = "ring")]
16 Signing(ring::error::Unspecified),
17 /// If we encouter a SSL error, for example an invalid certificate, this variant is used.
18 #[cfg(feature = "openssl")]
19 Ssl(openssl::error::ErrorStack),
20 /// If we have problems creating or parsing a json web token, this variant is used.
21 Jwt(jsonwebtoken::errors::Error),
22 /// If we cannot deserialize one of the repsonses sent by Google, this variant is used.
23 Serialization(serde_json::error::Error),
24 /// If another failure causes the error, this variant is populated.
25 Other(String),
26}
27
28impl Error {
29 pub(crate) fn new(msg: &str) -> Error {
30 Error::Other(msg.to_string())
31 }
32}
33
34impl std::fmt::Display for Error {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
36 writeln!(f, "{:?}", self)
37 }
38}
39
40impl std::error::Error for Error {
41 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
42 match self {
43 Self::Google(e) => Some(e),
44 Self::Reqwest(e) => Some(e),
45 #[cfg(feature = "openssl")]
46 Self::Ssl(e) => Some(e),
47 #[cfg(feature = "ring")]
48 Self::Pem(e) => Some(e),
49 #[cfg(feature = "ring")]
50 Self::KeyRejected(e) => Some(e),
51 #[cfg(feature = "ring")]
52 Self::Signing(e) => Some(e),
53 Self::Jwt(e) => Some(e),
54 Self::Serialization(e) => Some(e),
55 Self::Other(_) => None,
56 }
57 }
58}
59
60impl From<reqwest::Error> for Error {
61 fn from(err: reqwest::Error) -> Self {
62 Self::Reqwest(err)
63 }
64}
65
66#[cfg(feature = "openssl")]
67impl From<openssl::error::ErrorStack> for Error {
68 fn from(err: openssl::error::ErrorStack) -> Self {
69 Self::Ssl(err)
70 }
71}
72
73#[cfg(feature = "ring")]
74impl From<pem::PemError> for Error {
75 fn from(err: pem::PemError) -> Self {
76 Self::Pem(err)
77 }
78}
79
80#[cfg(feature = "ring")]
81impl From<ring::error::KeyRejected> for Error {
82 fn from(err: ring::error::KeyRejected) -> Self {
83 Self::KeyRejected(err)
84 }
85}
86
87#[cfg(feature = "ring")]
88impl From<ring::error::Unspecified> for Error {
89 fn from(err: ring::error::Unspecified) -> Self {
90 Self::Signing(err)
91 }
92}
93
94impl From<jsonwebtoken::errors::Error> for Error {
95 fn from(err: jsonwebtoken::errors::Error) -> Self {
96 Self::Jwt(err)
97 }
98}
99
100impl From<serde_json::error::Error> for Error {
101 fn from(err: serde_json::error::Error) -> Self {
102 Self::Serialization(err)
103 }
104}
105
106impl From<reqwest::header::InvalidHeaderValue> for Error {
107 fn from(err: reqwest::header::InvalidHeaderValue) -> Self {
108 Self::Other(err.to_string())
109 }
110}
111
112impl From<std::io::Error> for Error {
113 fn from(err: std::io::Error) -> Self {
114 Self::Other(err.to_string())
115 }
116}
117
118#[derive(Debug, serde::Deserialize)]
119#[serde(rename = "camelCase")]
120#[serde(untagged)]
121pub(crate) enum GoogleResponse<T> {
122 Success(T),
123 Error(GoogleErrorResponse),
124}
125
126// TODO comment this in when try_trait (#42327) get stabilized and enjoy the nicer handling of
127// errors
128//
129// impl<T> std::ops::Try for GoogleResponse<T> {
130// type Ok = T;
131// type Error = Error;
132//
133// fn into_result(self) -> Result<Self::Ok, Error> {
134// match self {
135// GoogleResponse::Success(t) => Ok(t),
136// GoogleResponse::Error(error) => Err(Error::Google(error)),
137// }
138// }
139//
140// fn from_error(_a: Error) -> Self {
141// unimplemented!()
142// }
143//
144// fn from_ok(t: T) -> Self {
145// GoogleResponse::Success(t)
146// }
147// }
148
149/// The structure of a error response returned by Google.
150#[derive(Debug, serde::Deserialize)]
151#[serde(rename = "camelCase")]
152pub struct GoogleErrorResponse {
153 /// A container for the error information.
154 pub error: ErrorList,
155}
156
157impl GoogleErrorResponse {
158 /// Return list of errors returned by Google
159 pub fn errors(&self) -> &[GoogleError] {
160 &self.error.errors
161 }
162
163 /// Check whether errors contain given reason
164 pub fn errors_has_reason(&self, reason: &Reason) -> bool {
165 self.errors()
166 .iter()
167 .any(|google_error| google_error.is_reason(reason))
168 }
169}
170
171impl std::fmt::Display for GoogleErrorResponse {
172 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
173 writeln!(f, "{:?}", self)
174 }
175}
176
177impl std::error::Error for GoogleErrorResponse {}
178
179/// A container for the error information.
180#[derive(Debug, serde::Deserialize)]
181#[serde(rename = "camelCase")]
182pub struct ErrorList {
183 /// A container for the error details.
184 pub errors: Vec<GoogleError>,
185 /// An HTTP status code value, without the textual description.
186 ///
187 /// Example values include: 400 (Bad Request), 401 (Unauthorized), and 404 (Not Found).
188 pub code: u16,
189 /// Description of the error. Same as errors.message.
190 pub message: String,
191}
192
193/// Google Error structure
194#[derive(Debug, serde::Deserialize)]
195#[serde(rename = "camelCase")]
196pub struct GoogleError {
197 /// The scope of the error. Example values include: global and push.
198 pub domain: String,
199 /// Example values include `invalid`, `invalidParameter`, and `required`.
200 pub reason: Reason,
201 /// Description of the error.
202 ///
203 /// Example values include `Invalid argument`, `Login required`, and `Required parameter:
204 /// project`.
205 pub message: String,
206 /// The location or part of the request that caused the error. Use with `location` to pinpoint
207 /// the error. For example, if you specify an invalid value for a parameter, the `locationType`
208 /// will be parameter and the location will be the name of the parameter.
209 ///
210 /// Example values include `header` and `parameter`.
211 pub location_type: Option<String>,
212 /// The specific item within the `locationType` that caused the error. For example, if you
213 /// specify an invalid value for a parameter, the `location` will be the name of the parameter.
214 ///
215 /// Example values include: `Authorization`, `project`, and `projection`.
216 pub location: Option<String>,
217}
218
219impl std::fmt::Display for GoogleError {
220 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
221 write!(f, "{}", self.message)
222 }
223}
224
225impl std::error::Error for GoogleError {}
226
227impl GoogleError {
228 /// Check what was the reason of error
229 pub fn is_reason(&self, reason: &Reason) -> bool {
230 self.reason == *reason
231 }
232}
233
234impl From<GoogleErrorResponse> for Error {
235 fn from(err: GoogleErrorResponse) -> Self {
236 Self::Google(err)
237 }
238}
239
240/// Google provides a list of codes, but testing indicates that this list is not exhaustive.
241#[derive(Debug, PartialEq, serde::Deserialize)]
242#[serde(rename_all = "camelCase")]
243pub enum Reason {
244 /// When requesting a download using alt=media URL parameter, the direct URL path to use is
245 /// prefixed by /download. If this is omitted, the service will issue this redirect with the
246 /// appropriate media download path in the Location header.
247 MediaDownloadRedirect,
248 /// The conditional request would have been successful, but the condition was false, so no body
249 /// was sent.
250 NotModified,
251 /// Resource temporarily located elsewhere according to the Location header. Among other
252 /// reasons, this can occur when cookie-based authentication is being used, e.g., when using the
253 /// Storage Browser, and it receives a request to download content.
254 TemporaryRedirect,
255 // /// Indicates an incomplete resumable upload and provides the range of bytes already received by
256 // /// Cloud Storage. Responses with this status do not contain a body.
257 // ResumeIncomplete,
258
259 // <bad requests>
260 /// Undocumeten variant that is sometimes returned by Google.
261 Invalid,
262 /// The request cannot be completed based on your current Cloud Storage settings. For example,
263 /// you cannot lock a retention policy if the requested bucket doesn't have a retention policy,
264 /// and you cannot set ACLs if the requested bucket has Bucket Policy Only enabled.
265 BadRequest,
266 /// The retention period on a locked bucket cannot be reduced.
267 BadRequestException,
268 /// Bad Cloud KMS key.
269 CloudKmsBadKey,
270 /// Cloud KMS key name cannot be changed.
271 CloudKmsCannotChangeKeyName,
272 /// Resource's Cloud KMS decryption key not found.
273 CloudKmsDecryptionKeyNotFound,
274 /// Cloud KMS key is disabled, destroyed, or scheduled to be destroyed.
275 CloudKmsDisabledKey,
276 /// Cloud KMS encryption key not found.
277 CloudKmsEncryptionKeyNotFound,
278 /// Cloud KMS key location not allowed.
279 CloudKmsKeyLocationNotAllowed,
280 /// Missing an encryption algorithm, or the provided algorithm is not "AE256."
281 CustomerEncryptionAlgorithmIsInvalid,
282 /// Missing an encryption key, or it is not Base64 encoded, or it does not meet the required
283 /// length of the encryption algorithm.
284 CustomerEncryptionKeyFormatIsInvalid,
285 /// The provided encryption key is incorrect.
286 CustomerEncryptionKeyIsIncorrect,
287 /// Missing a SHA256 hash of the encryption key, or it is not Base64 encoded, or it does not
288 /// match the encryption key.
289 CustomerEncryptionKeySha256IsInvalid,
290 /// The value for the alt URL parameter was not recognized.
291 InvalidAltValue,
292 /// The value for one of fields in the request body was invalid.
293 InvalidArgument,
294 /// The value for one of the URL parameters was invalid. In addition to normal URL parameter
295 /// validation, any URL parameters that have a corresponding value in provided JSON request
296 /// bodies must match if they are both specified. If using JSONP, you will get this error if you
297 /// provide an alt parameter that is not json.
298 InvalidParameter,
299 /// Uploads or normal API request was sent to a `/download/*` path. Use the same path, but
300 /// without the /download prefix.
301 NotDownload,
302 /// Downloads or normal API request was sent to an `/upload/*` path. Use the same path, but
303 /// without the `/upload` prefix.
304 NotUpload,
305 /// Could not parse the body of the request according to the provided Content-Type.
306 ParseError,
307 /// Channel id must match the following regular expression: `[A-Za-z0-9\\-_\\+/=]+`.
308 #[serde(rename = "push.channelIdInvalid")]
309 PushChannelIdInvalid,
310 /// `storage.objects.watchAll`'s id property must be unique across channels.
311 #[serde(rename = "push.channelIdNotUnique")]
312 PushChannelIdNotUnique,
313 /// `storage.objects.watchAll`'s address property must contain a valid URL.
314 #[serde(rename = "push.webhookUrlNoHostOrAddress")]
315 PushWebhookUrlNoHostOrAddress,
316 /// `storage.objects.watchAll`'s address property must be an HTTPS URL.
317 #[serde(rename = "push.webhookUrlNotHttps")]
318 PushWebhookUrlNotHttps,
319 /// A required URL parameter or required request body JSON property is missing.
320 Required,
321 /// The resource is encrypted with a customer-supplied encryption key, but the request did not
322 /// provide one.
323 ResourceIsEncryptedWithCustomerEncryptionKey,
324 /// The resource is not encrypted with a customer-supplied encryption key, but the request
325 /// provided one.
326 ResourceNotEncryptedWithCustomerEncryptionKey,
327 /// A request was made to an API version that has been turned down. Clients will need to update
328 /// to a supported version.
329 TurnedDown,
330 /// The user project specified in the request does not match the user project specifed in an
331 /// earlier, related request.
332 UserProjectInconsistent,
333 /// The user project specified in the request is invalid, either because it is a malformed
334 /// project id or because it refers to a non-existent project.
335 UserProjectInvalid,
336 /// The requested bucket has Requester Pays enabled, the requester is not an owner of the
337 /// bucket, and no user project was present in the request.
338 UserProjectMissing,
339 /// storage.objects.insert must be invoked as an upload rather than a metadata.
340 WrongUrlForUpload,
341 // </bad requests>
342
343 // <unauthorized>
344 /// Access to a Requester Pays bucket requires authentication.
345 #[serde(rename = "AuthenticationRequiredRequesterPays")]
346 AuthenticationRequiredRequesterPays,
347 /// This error indicates a problem with the authorization provided in the request to Cloud
348 /// Storage. The following are some situations where that will occur:
349 ///
350 /// * The OAuth access token has expired and needs to be refreshed. This can be avoided by
351 /// refreshing the access token early, but code can also catch this error, refresh the token
352 /// and retry automatically.
353 /// * Multiple non-matching authorizations were provided; choose one mode only.
354 /// * The OAuth access token's bound project does not match the project associated with the
355 /// provided developer key.
356 /// * The Authorization header was of an unrecognized format or uses an unsupported credential
357 /// type.
358 AuthError,
359 /// When downloading content from a cookie-authenticated site, e.g., using the Storage Browser,
360 /// the response will redirect to a temporary domain. This error will occur if access to said
361 /// domain occurs after the domain expires. Issue the original request again, and receive a new
362 /// redirect.
363 LockedDomainExpired,
364 /// Requests to storage.objects.watchAll will fail unless you verify you own the domain.
365 #[serde(rename = "push.webhookUrlUnauthorized")]
366 PushWebhookUrlUnauthorized,
367 // /// Access to a non-public method that requires authorization was made, but none was provided in
368 // /// the Authorization header or through other means.
369 // Required,
370 // </unauthorized>
371
372 // <forbidden>
373 /// The account associated with the project that owns the bucket or object has been disabled. Check the Google Cloud Console to see if there is a problem with billing, and if not, contact account support.
374 AccountDisabled,
375 /// The Cloud Storage JSON API is restricted by law from operating with certain countries.
376 CountryBlocked,
377 /// According to access control policy, the current user does not have access to perform the requested action. This code applies even if the resource being acted on doesn't exist.
378 Forbidden,
379 /// According to access control policy, the current user does not have access to perform the requested action. This code applies even if the resource being acted on doesn't exist.
380 InsufficientPermissions,
381 /// Object overwrite or deletion is not allowed due to an active hold on the object.
382 ObjectUnderActiveHold,
383 /// The Cloud Storage rate limit was exceeded. Retry using exponential backoff.
384 RateLimitExceeded,
385 /// Object overwrite or deletion is not allowed until the object meets the retention period set by the retention policy on the bucket.
386 RetentionPolicyNotMet,
387 /// Requests to this API require SSL.
388 SslRequired,
389 /// Calls to storage.channels.stop require that the caller own the channel.
390 StopChannelCallerNotOwner,
391 /// This error implies that for the project associated with the OAuth token or the developer key provided, access to Cloud Storage JSON API is not enabled. This is most commonly because Cloud Storage JSON API is not enabled in the Google Cloud Console, though there are other cases where the project is blocked or has been deleted when this can occur.
392 #[serde(rename = "UsageLimits.accessNotConfigured")]
393 UsageLimitsAccessNotConfigured,
394 /// The requester is not authorized to use the project specified in their request. The
395 /// requester must have either the serviceusage.services.use permission or the Editor role for
396 /// the specified project.
397 #[serde(rename = "UserProjectAccessDenied")]
398 UserProjectAccessDenied,
399 /// There is a problem with the project used in the request that prevents the operation from
400 /// completing successfully. One issue could be billing. Check the billing page to see if you
401 /// have a past due balance or if the credit card (or other payment mechanism) on your account is expired. For project creation, see the Projects page in the Google Cloud Console. For other problems, see the Resources and Support page.
402 #[serde(rename = "UserProjectAccountProblem")]
403 UserProjectAccountProblem,
404 /// The developer-specified per-user rate quota was exceeded. If you are the developer, then
405 /// you can view these quotas at Quotas pane in the Google Cloud Console.
406 UserRateLimitExceeded,
407 /// Seems to indicate the same thing
408 // NONEXHAUST
409 QuotaExceeded,
410 // </forbidden>
411 /// Either there is no API method associated with the URL path of the request, or the request
412 /// refers to one or more resources that were not found.
413 NotFound,
414 /// Either there is no API method associated with the URL path of the request, or the request
415 /// refers to one or more resources that were not found.
416 MethodNotAllowed,
417 /// The request timed out. Please try again using truncated exponential backoff.
418 UploadBrokenConnection,
419 /// A request to change a resource, usually a storage.*.update or storage.*.patch method, failed
420 /// to commit the change due to a conflicting concurrent change to the same resource. The
421 /// request can be retried, though care should be taken to consider the new state of the
422 /// resource to avoid blind overwriting of other agent's changes.
423 Conflict,
424 /// You have attempted to use a resumable upload session that is no longer available. If the
425 /// reported status code was not successful and you still wish to upload the file, you must
426 /// start a new session.
427 Gone,
428 // /// You must provide the Content-Length HTTP header. This error has no response body.
429 // LengthRequired,
430
431 // <precondition failed>
432 /// At least one of the pre-conditions you specified did not hold.
433 ConditionNotMet,
434 /// Request violates an OrgPolicy constraint.
435 OrgPolicyConstraintFailed,
436 // </precondition failed>
437 /// The Cloud Storage JSON API supports up to 5 TB objects.
438 ///
439 /// This error may, alternatively, arise if copying objects between locations and/or storage
440 /// classes can not complete within 30 seconds. In this case, use the `Object::rewrite` method
441 /// instead.
442 UploadTooLarge,
443 /// The requested Range cannot be satisfied.
444 RequestedRangeNotSatisfiable,
445 /// A [Cloud Storage JSON API usage limit](https://cloud.google.com/storage/quotas) was
446 /// exceeded. If your application tries to use more than its limit, additional requests will
447 /// fail. Throttle your client's requests, and/or use truncated exponential backoff.
448 #[serde(rename = "usageLimits.rateLimitExceeded")]
449 UsageLimitsRateLimitExceeded,
450
451 // <internal server error>
452 /// We encountered an internal error. Please try again using truncated exponential backoff.
453 BackendError,
454 /// We encountered an internal error. Please try again using truncated exponential backoff.
455 InternalError,
456 // </internal server error>
457 /// May be returned by Google, meaning undocumented.
458 // NONEXHAUST
459 GatewayTimeout,
460}
461
462#[derive(Debug, serde::Deserialize)]
463#[serde(rename = "camelCase")]
464enum BadRequest {}
465
466#[derive(Debug, serde::Deserialize)]
467#[serde(rename = "camelCase")]
468enum Unauthorized {}
469
470#[derive(Debug, serde::Deserialize)]
471#[serde(rename = "camelCase")]
472enum Forbidden {}
473
474#[derive(Debug, serde::Deserialize)]
475#[serde(rename = "camelCase")]
476enum PreconditionFailed {}
477
478#[derive(Debug, serde::Deserialize)]
479#[serde(rename = "camelCase")]
480enum InternalServerError {}