Skip to content

Commit

Permalink
Merge pull request #961 from dhermes/jgeewax-937-more-helpful-errors
Browse files Browse the repository at this point in the history
Fixes #937 - Adds HTTP method and URL to exceptions.
  • Loading branch information
dhermes committed Jul 3, 2015
2 parents bb25b9e + e5b0edf commit af97ff1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion gcloud/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ def api_request(self, method, path, query_params=None,
target_object=_target_object)

if not 200 <= response.status < 300:
raise make_exception(response, content)
raise make_exception(response, content,
error_info=method + ' ' + url)

string_or_bytes = (six.binary_type, six.text_type)
if content and expect_json and isinstance(content, string_or_bytes):
Expand Down
9 changes: 8 additions & 1 deletion gcloud/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class ServiceUnavailable(ServerError):
code = 503


def make_exception(response, content, use_json=True):
def make_exception(response, content, error_info=None, use_json=True):
"""Factory: create exception based on HTTP response code.
:type response: :class:`httplib2.Response` or other HTTP response object
Expand All @@ -167,6 +167,10 @@ def make_exception(response, content, use_json=True):
:type content: string or dictionary
:param content: The body of the HTTP error response.
:type error_info: string
:param error_info: Optional string giving extra information about the
failed request.
:type use_json: boolean
:param use_json: Flag indicating if ``content`` is expected to be JSON.
Expand All @@ -187,6 +191,9 @@ def make_exception(response, content, use_json=True):
message = payload.get('error', {}).get('message', '')
errors = payload.get('error', {}).get('errors', ())

if error_info is not None:
message += ' (%s)' % (error_info,)

try:
klass = _HTTP_CODE_TO_EXCEPTION[response.status]
except KeyError:
Expand Down

0 comments on commit af97ff1

Please sign in to comment.