google_cloud_storage/http/
error.rs

1use std::error::Error;
2use std::fmt;
3
4/// An error response returned from Google Cloud Storage.
5///
6/// See the [`HTTP status and error codes for JSON`][1] documentation for more details.
7///
8/// [1]: https://cloud.google.com/storage/docs/json_api/v1/status-codes
9#[derive(Debug, serde::Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct ErrorResponse {
12    /// An HTTP status value, without the textual description.
13    ///
14    /// Example values include: `400` (Bad Request), `401` (Unauthorized), and `404` (Not Found).
15    pub code: u16,
16
17    /// A container for the error details.
18    pub errors: Vec<ErrorResponseItem>,
19
20    /// Description of the error. Same as `errors.message`.
21    pub message: String,
22}
23
24impl ErrorResponse {
25    /// Returns `true` if the error is retriable according to the [GCS documentation][1].
26    ///
27    /// [1]: https://cloud.google.com/storage/docs/retry-strategy#retryable
28    pub fn is_retriable(&self) -> bool {
29        matches!(self.code, 408 | 429 | 500..=599)
30    }
31}
32
33impl fmt::Display for ErrorResponse {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        self.message.fmt(f)
36    }
37}
38
39impl Error for ErrorResponse {}
40
41#[derive(Debug, serde::Deserialize)]
42#[serde(rename_all = "camelCase")]
43pub struct ErrorResponseItem {
44    /// The scope of the error. Example values include: `global` and `push`.
45    pub domain: String,
46
47    /// The specific item within the `locationType` that caused the error. For example, if you
48    /// specify an invalid value for a parameter, the `location` will be the name of the parameter.
49    ///
50    /// Example values include: `Authorization`, `project`, and `projection`.
51    pub location: Option<String>,
52
53    /// The location or part of the request that caused the error. Use with `location` to pinpoint
54    /// the error. For example, if you specify an invalid value for a parameter, the `locationType`
55    /// will be `parameter` and the `location` will be the name of the parameter.
56    ///
57    /// Example values include `header` and `parameter`.
58    pub location_type: Option<String>,
59
60    /// Description of the error.
61    ///
62    /// Example values include `Invalid argument`, `Login required`, and
63    /// `Required parameter: project`.
64    pub message: String,
65
66    /// Example values include `invalid`, `invalidParameter`, and `required`.
67    pub reason: String,
68}
69
70impl fmt::Display for ErrorResponseItem {
71    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72        self.message.fmt(f)
73    }
74}
75
76/// The GCS error response JSON format contains an extra object level that is inconvenient to include in our
77/// error.
78#[derive(serde::Deserialize)]
79pub(crate) struct ErrorWrapper {
80    pub(crate) error: ErrorResponse,
81}