Skip to content

Commit

Permalink
feat: error message return from api (#235)
Browse files Browse the repository at this point in the history
* feat(storage): error message retyrn from api

* feat: add comment for clarification

* fix: remove unknown error

Co-authored-by: Tres Seaver <tseaver@palladion.com>
Co-authored-by: Frank Natividad <frankyn@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 26, 2020
1 parent 3465d08 commit a8de586
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
8 changes: 7 additions & 1 deletion google/cloud/storage/blob.py
Expand Up @@ -3427,7 +3427,13 @@ def _raise_from_invalid_response(error):
to the failed status code
"""
response = error.response
error_message = str(error)

# The 'response.text' gives the actual reason of error, where 'error' gives
# the message of expected status code.
if response.text:
error_message = response.text + ": " + str(error)
else:
error_message = str(error)

message = u"{method} {url}: {error}".format(
method=response.request.method, url=response.request.url, error=error_message
Expand Down
12 changes: 9 additions & 3 deletions tests/unit/test_blob.py
Expand Up @@ -4356,14 +4356,15 @@ def _call_fut(error):

return _raise_from_invalid_response(error)

def _helper(self, message, code=http_client.BAD_REQUEST, args=()):
def _helper(self, message, code=http_client.BAD_REQUEST, reason=None, args=()):
import requests

from google.resumable_media import InvalidResponse
from google.api_core import exceptions

response = requests.Response()
response.request = requests.Request("GET", "http://example.com").prepare()
response._content = reason
response.status_code = code
error = InvalidResponse(response, message, *args)

Expand All @@ -4381,9 +4382,14 @@ def test_default(self):

def test_w_206_and_args(self):
message = "Failure"
reason = b"Not available"
args = ("one", "two")
exc_info = self._helper(message, code=http_client.PARTIAL_CONTENT, args=args)
expected = "GET http://example.com/: {}".format((message,) + args)
exc_info = self._helper(
message, code=http_client.PARTIAL_CONTENT, reason=reason, args=args
)
expected = "GET http://example.com/: {}: {}".format(
reason.decode("utf-8"), (message,) + args
)
self.assertEqual(exc_info.exception.message, expected)
self.assertEqual(exc_info.exception.errors, [])

Expand Down

0 comments on commit a8de586

Please sign in to comment.