| OLD | NEW |
| (Empty) | |
| 1 # Copyright 2012 Google Inc. All Rights Reserved. |
| 2 |
| 3 """Google Cloud Storage specific Files API calls.""" |
| 4 |
| 5 |
| 6 |
| 7 |
| 8 |
| 9 __all__ = ['AuthorizationError', |
| 10 'check_status', |
| 11 'Error', |
| 12 'FatalError', |
| 13 'ForbiddenError', |
| 14 'NotFoundError', |
| 15 'ServerError', |
| 16 'TimeoutError', |
| 17 'TransientError', |
| 18 ] |
| 19 |
| 20 import httplib |
| 21 |
| 22 |
| 23 class Error(Exception): |
| 24 """Base error for all gcs operations. |
| 25 |
| 26 Error can happen on GAE side or GCS server side. |
| 27 For details on a particular GCS HTTP response code, see |
| 28 https://developers.google.com/storage/docs/reference-status#standardcodes |
| 29 """ |
| 30 |
| 31 |
| 32 class TransientError(Error): |
| 33 """TransientError could be retried.""" |
| 34 |
| 35 |
| 36 class TimeoutError(TransientError): |
| 37 """HTTP 408 timeout.""" |
| 38 |
| 39 |
| 40 class FatalError(Error): |
| 41 """FatalError shouldn't be retried.""" |
| 42 |
| 43 |
| 44 class NotFoundError(FatalError): |
| 45 """HTTP 404 resource not found.""" |
| 46 |
| 47 |
| 48 class ForbiddenError(FatalError): |
| 49 """HTTP 403 Forbidden. |
| 50 |
| 51 While GCS replies with a 403 error for many reasons, the most common one |
| 52 is due to bucket permission not correctly setup for your app to access. |
| 53 """ |
| 54 |
| 55 |
| 56 class AuthorizationError(FatalError): |
| 57 """HTTP 401 authentication required. |
| 58 |
| 59 Unauthorized request has been received by GCS. |
| 60 |
| 61 This error is mostly handled by GCS client. GCS client will request |
| 62 a new access token and retry the request. |
| 63 """ |
| 64 |
| 65 |
| 66 class InvalidRange(FatalError): |
| 67 """HTTP 416 RequestRangeNotSatifiable.""" |
| 68 |
| 69 |
| 70 class ServerError(TransientError): |
| 71 """HTTP >= 500 server side error.""" |
| 72 |
| 73 |
| 74 def check_status(status, expected, path, headers=None, |
| 75 resp_headers=None, extras=None): |
| 76 """Check HTTP response status is expected. |
| 77 |
| 78 Args: |
| 79 status: HTTP response status. int. |
| 80 expected: a list of expected statuses. A list of ints. |
| 81 path: filename or a path prefix. |
| 82 headers: HTTP request headers. |
| 83 resp_headers: HTTP response headers. |
| 84 extras: extra info to be logged verbatim if error occurs. |
| 85 |
| 86 Raises: |
| 87 AuthorizationError: if authorization failed. |
| 88 NotFoundError: if an object that's expected to exist doesn't. |
| 89 TimeoutError: if HTTP request timed out. |
| 90 ServerError: if server experienced some errors. |
| 91 FatalError: if any other unexpected errors occurred. |
| 92 """ |
| 93 if status in expected: |
| 94 return |
| 95 |
| 96 msg = ('Expect status %r from Google Storage. But got status %d.\n' |
| 97 'Path: %r.\n' |
| 98 'Request headers: %r.\n' |
| 99 'Response headers: %r.\n' |
| 100 'Extra info: %r.\n' % |
| 101 (expected, status, path, headers, resp_headers, extras)) |
| 102 |
| 103 if status == httplib.UNAUTHORIZED: |
| 104 raise AuthorizationError(msg) |
| 105 elif status == httplib.FORBIDDEN: |
| 106 raise ForbiddenError(msg) |
| 107 elif status == httplib.NOT_FOUND: |
| 108 raise NotFoundError(msg) |
| 109 elif status == httplib.REQUEST_TIMEOUT: |
| 110 raise TimeoutError(msg) |
| 111 elif status == httplib.REQUESTED_RANGE_NOT_SATISFIABLE: |
| 112 raise InvalidRange(msg) |
| 113 elif status >= 500: |
| 114 raise ServerError(msg) |
| 115 else: |
| 116 raise FatalError(msg) |
| OLD | NEW |