Skip to content
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

feat: error message return from api #235

Merged
merged 10 commits into from Aug 26, 2020
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