Skip to content
Permalink
Browse files
feat: error message return from api (#235)
* 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 a8de5868f32b45868f178f420138fcd2fe42f5fd
Showing with 16 additions and 4 deletions.
  1. +7 −1 google/cloud/storage/blob.py
  2. +9 −3 tests/unit/test_blob.py
@@ -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
@@ -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)

@@ -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, [])

0 comments on commit a8de586

Please sign in to comment.