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