-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Fix #164: Map HTTP error responses for storage onto specific exception classes. #355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tseaver
merged 2 commits into
googleapis:master
from
tseaver:164-add_http_specific_exceptions
Nov 7, 2014
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,170 @@ | ||
| """Custom exceptions for gcloud.storage package.""" | ||
| """Custom exceptions for gcloud.storage package. | ||
|
|
||
| See: https://cloud.google.com/storage/docs/json_api/v1/status-codes | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| _HTTP_CODE_TO_EXCEPTION = {} # populated at end of module | ||
|
|
||
|
|
||
| class StorageError(Exception): | ||
| """Base error class for gcloud errors.""" | ||
| """Base error class for gcloud errors (abstract). | ||
|
|
||
| Each subclass represents a single type of HTTP error response. | ||
| """ | ||
| code = None | ||
| """HTTP status code. Concrete subclasses *must* define. | ||
|
|
||
| class ConnectionError(StorageError): | ||
| """Exception corresponding to a bad HTTP/RPC connection.""" | ||
| See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html | ||
| """ | ||
|
|
||
| def __init__(self, response, content): | ||
| message = str(response) + content | ||
| super(ConnectionError, self).__init__(message) | ||
| def __init__(self, message, errors=()): | ||
| super(StorageError, self).__init__() | ||
| # suppress deprecation warning under 2.6.x | ||
| self.message = message | ||
| self._errors = [error.copy() for error in errors] | ||
|
|
||
| def __str__(self): | ||
| return '%d %s' % (self.code, self.message) | ||
|
|
||
| class NotFoundError(StorageError): | ||
| """Exception corresponding to a 404 not found bad connection.""" | ||
| @property | ||
| def errors(self): | ||
| """Detailed error information. | ||
|
|
||
| def __init__(self, response): | ||
| super(NotFoundError, self).__init__('') | ||
| # suppress deprecation warning under 2.6.x | ||
| self.message = 'Request returned a 404. Headers: %s' % (response,) | ||
| :rtype: list(dict) | ||
| :returns: a list of mappings describing each error. | ||
| """ | ||
| return [error.copy() for error in self._errors] | ||
|
|
||
|
|
||
| class Redirection(StorageError): | ||
| """Base for 3xx responses | ||
|
|
||
| This class is abstract. | ||
| """ | ||
|
|
||
|
|
||
| class MovedPermanently(Redirection): | ||
| """Exception mapping a '301 Moved Permanently' response.""" | ||
| code = 301 | ||
|
|
||
|
|
||
| class NotModified(Redirection): | ||
| """Exception mapping a '304 Not Modified' response.""" | ||
| code = 304 | ||
|
|
||
|
|
||
| class TemporaryRedirect(Redirection): | ||
| """Exception mapping a '307 Temporary Redirect' response.""" | ||
| code = 307 | ||
|
|
||
|
|
||
| class ResumeIncomplete(Redirection): | ||
| """Exception mapping a '308 Resume Incomplete' response.""" | ||
| code = 308 | ||
|
|
||
|
|
||
| class ClientError(StorageError): | ||
| """Base for 4xx responses | ||
|
|
||
| This class is abstract | ||
| """ | ||
|
|
||
|
|
||
| class BadRequest(ClientError): | ||
| """Exception mapping a '400 Bad Request' response.""" | ||
| code = 400 | ||
|
|
||
|
|
||
| class Unauthorized(ClientError): | ||
| """Exception mapping a '401 Unauthorized' response.""" | ||
| code = 400 | ||
|
|
||
|
|
||
| class Forbidden(ClientError): | ||
| """Exception mapping a '403 Forbidden' response.""" | ||
| code = 400 | ||
|
|
||
|
|
||
| class NotFound(ClientError): | ||
| """Exception mapping a '404 Not Found' response.""" | ||
| code = 404 | ||
|
|
||
|
|
||
| class MethodNotAllowed(ClientError): | ||
| """Exception mapping a '405 Method Not Allowed' response.""" | ||
| code = 405 | ||
|
|
||
|
|
||
| class Conflict(ClientError): | ||
| """Exception mapping a '409 Conflict' response.""" | ||
| code = 409 | ||
|
|
||
|
|
||
| class LengthRequired(ClientError): | ||
| """Exception mapping a '411 Length Required' response.""" | ||
| code = 411 | ||
|
|
||
|
|
||
| class PreconditionFailed(ClientError): | ||
| """Exception mapping a '412 Precondition Failed' response.""" | ||
| code = 412 | ||
|
|
||
|
|
||
| class RequestRangeNotSatisfiable(ClientError): | ||
| """Exception mapping a '416 Request Range Not Satisfiable' response.""" | ||
| code = 416 | ||
|
|
||
|
|
||
| class TooManyRequests(ClientError): | ||
| """Exception mapping a '429 Too Many Requests' response.""" | ||
| code = 429 | ||
|
|
||
|
|
||
| class ServerError(StorageError): | ||
| """Base for 5xx responses: (abstract)""" | ||
|
|
||
|
|
||
| class InternalServerError(ServerError): | ||
| """Exception mapping a '500 Internal Server Error' response.""" | ||
| code = 500 | ||
|
|
||
|
|
||
| class NotImplemented(ServerError): | ||
| """Exception mapping a '501 Not Implemented' response.""" | ||
| code = 501 | ||
|
|
||
|
|
||
| class ServiceUnavailable(ServerError): | ||
| """Exception mapping a '503 Service Unavailable' response.""" | ||
| code = 503 | ||
|
|
||
|
|
||
| def make_exception(response, content): | ||
| """Factory: create exception based on HTTP response code. | ||
|
|
||
| :rtype: instance of :class:`StorageError`, or a concrete subclass. | ||
| """ | ||
|
|
||
| if isinstance(content, str): | ||
| content = json.loads(content) | ||
|
|
||
| message = content.get('message') | ||
| error = content.get('error', {}) | ||
| errors = error.get('errors', ()) | ||
|
|
||
| try: | ||
| klass = _HTTP_CODE_TO_EXCEPTION[response.status] | ||
| except KeyError: | ||
| error = StorageError(message, errors) | ||
| error.code = response.status | ||
| else: | ||
| error = klass(message, errors) | ||
| return error | ||
|
|
||
|
|
||
| for name, value in globals().items(): | ||
| code = getattr(value, 'code', None) | ||
| if code is not None: | ||
| _HTTP_CODE_TO_EXCEPTION[code] = value | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.