|
1 | | -"""Custom exceptions for gcloud.storage package.""" |
| 1 | +"""Custom exceptions for gcloud.storage package. |
| 2 | +
|
| 3 | +See: https://cloud.google.com/storage/docs/json_api/v1/status-codes |
| 4 | +""" |
| 5 | + |
| 6 | +import json |
| 7 | + |
| 8 | +_HTTP_CODE_TO_EXCEPTION = {} # populated at end of module |
2 | 9 |
|
3 | 10 |
|
4 | 11 | class StorageError(Exception): |
5 | | - """Base error class for gcloud errors.""" |
| 12 | + """Base error class for gcloud errors (abstract). |
6 | 13 |
|
| 14 | + Each subclass represents a single type of HTTP error response. |
| 15 | + """ |
| 16 | + code = None |
| 17 | + """HTTP status code. Concrete subclasses *must* define. |
7 | 18 |
|
8 | | -class ConnectionError(StorageError): |
9 | | - """Exception corresponding to a bad HTTP/RPC connection.""" |
| 19 | + See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
| 20 | + """ |
10 | 21 |
|
11 | | - def __init__(self, response, content): |
12 | | - message = str(response) + content |
13 | | - super(ConnectionError, self).__init__(message) |
| 22 | + def __init__(self, message, errors=()): |
| 23 | + super(StorageError, self).__init__() |
14 | 24 | # suppress deprecation warning under 2.6.x |
15 | 25 | self.message = message |
| 26 | + self._errors = [error.copy() for error in errors] |
16 | 27 |
|
| 28 | + def __str__(self): |
| 29 | + return '%d %s' % (self.code, self.message) |
17 | 30 |
|
18 | | -class NotFoundError(StorageError): |
19 | | - """Exception corresponding to a 404 not found bad connection.""" |
| 31 | + @property |
| 32 | + def errors(self): |
| 33 | + """Detailed error information. |
20 | 34 |
|
21 | | - def __init__(self, response): |
22 | | - super(NotFoundError, self).__init__('') |
23 | | - # suppress deprecation warning under 2.6.x |
24 | | - self.message = 'Request returned a 404. Headers: %s' % (response,) |
| 35 | + :rtype: list(dict) |
| 36 | + :returns: a list of mappings describing each error. |
| 37 | + """ |
| 38 | + return [error.copy() for error in self._errors] |
| 39 | + |
| 40 | + |
| 41 | +class Redirection(StorageError): |
| 42 | + """Base for 3xx responses |
| 43 | +
|
| 44 | + This class is abstract. |
| 45 | + """ |
| 46 | + |
| 47 | + |
| 48 | +class MovedPermanently(Redirection): |
| 49 | + """Exception mapping a '301 Moved Permanently' response.""" |
| 50 | + code = 301 |
| 51 | + |
| 52 | + |
| 53 | +class NotModified(Redirection): |
| 54 | + """Exception mapping a '304 Not Modified' response.""" |
| 55 | + code = 304 |
| 56 | + |
| 57 | + |
| 58 | +class TemporaryRedirect(Redirection): |
| 59 | + """Exception mapping a '307 Temporary Redirect' response.""" |
| 60 | + code = 307 |
| 61 | + |
| 62 | + |
| 63 | +class ResumeIncomplete(Redirection): |
| 64 | + """Exception mapping a '308 Resume Incomplete' response.""" |
| 65 | + code = 308 |
| 66 | + |
| 67 | + |
| 68 | +class ClientError(StorageError): |
| 69 | + """Base for 4xx responses |
| 70 | +
|
| 71 | + This class is abstract |
| 72 | + """ |
| 73 | + |
| 74 | + |
| 75 | +class BadRequest(ClientError): |
| 76 | + """Exception mapping a '400 Bad Request' response.""" |
| 77 | + code = 400 |
| 78 | + |
| 79 | + |
| 80 | +class Unauthorized(ClientError): |
| 81 | + """Exception mapping a '401 Unauthorized' response.""" |
| 82 | + code = 400 |
| 83 | + |
| 84 | + |
| 85 | +class Forbidden(ClientError): |
| 86 | + """Exception mapping a '403 Forbidden' response.""" |
| 87 | + code = 400 |
| 88 | + |
| 89 | + |
| 90 | +class NotFound(ClientError): |
| 91 | + """Exception mapping a '404 Not Found' response.""" |
| 92 | + code = 404 |
| 93 | + |
| 94 | + |
| 95 | +class MethodNotAllowed(ClientError): |
| 96 | + """Exception mapping a '405 Method Not Allowed' response.""" |
| 97 | + code = 405 |
| 98 | + |
| 99 | + |
| 100 | +class Conflict(ClientError): |
| 101 | + """Exception mapping a '409 Conflict' response.""" |
| 102 | + code = 409 |
| 103 | + |
| 104 | + |
| 105 | +class LengthRequired(ClientError): |
| 106 | + """Exception mapping a '411 Length Required' response.""" |
| 107 | + code = 411 |
| 108 | + |
| 109 | + |
| 110 | +class PreconditionFailed(ClientError): |
| 111 | + """Exception mapping a '412 Precondition Failed' response.""" |
| 112 | + code = 412 |
| 113 | + |
| 114 | + |
| 115 | +class RequestRangeNotSatisfiable(ClientError): |
| 116 | + """Exception mapping a '416 Request Range Not Satisfiable' response.""" |
| 117 | + code = 416 |
| 118 | + |
| 119 | + |
| 120 | +class TooManyRequests(ClientError): |
| 121 | + """Exception mapping a '429 Too Many Requests' response.""" |
| 122 | + code = 429 |
| 123 | + |
| 124 | + |
| 125 | +class ServerError(StorageError): |
| 126 | + """Base for 5xx responses: (abstract)""" |
| 127 | + |
| 128 | + |
| 129 | +class InternalServerError(ServerError): |
| 130 | + """Exception mapping a '500 Internal Server Error' response.""" |
| 131 | + code = 500 |
| 132 | + |
| 133 | + |
| 134 | +class NotImplemented(ServerError): |
| 135 | + """Exception mapping a '501 Not Implemented' response.""" |
| 136 | + code = 501 |
| 137 | + |
| 138 | + |
| 139 | +class ServiceUnavailable(ServerError): |
| 140 | + """Exception mapping a '503 Service Unavailable' response.""" |
| 141 | + code = 503 |
| 142 | + |
| 143 | + |
| 144 | +def make_exception(response, content): |
| 145 | + """Factory: create exception based on HTTP response code. |
| 146 | +
|
| 147 | + :rtype: instance of :class:`StorageError`, or a concrete subclass. |
| 148 | + """ |
| 149 | + |
| 150 | + if isinstance(content, str): |
| 151 | + content = json.loads(content) |
| 152 | + |
| 153 | + message = content.get('message') |
| 154 | + error = content.get('error', {}) |
| 155 | + errors = error.get('errors', ()) |
| 156 | + |
| 157 | + try: |
| 158 | + klass = _HTTP_CODE_TO_EXCEPTION[response.status] |
| 159 | + except KeyError: |
| 160 | + error = StorageError(message, errors) |
| 161 | + error.code = response.status |
| 162 | + else: |
| 163 | + error = klass(message, errors) |
| 164 | + return error |
| 165 | + |
| 166 | + |
| 167 | +for name, value in globals().items(): |
| 168 | + code = getattr(value, 'code', None) |
| 169 | + if code is not None: |
| 170 | + _HTTP_CODE_TO_EXCEPTION[code] = value |
0 commit comments