Skip to content

Commit a8de586

Browse files
HemangChothanitseaverfrankyn
authored
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 <[email protected]> Co-authored-by: Frank Natividad <[email protected]>
1 parent 3465d08 commit a8de586

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

google/cloud/storage/blob.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3427,7 +3427,13 @@ def _raise_from_invalid_response(error):
34273427
to the failed status code
34283428
"""
34293429
response = error.response
3430-
error_message = str(error)
3430+
3431+
# The 'response.text' gives the actual reason of error, where 'error' gives
3432+
# the message of expected status code.
3433+
if response.text:
3434+
error_message = response.text + ": " + str(error)
3435+
else:
3436+
error_message = str(error)
34313437

34323438
message = u"{method} {url}: {error}".format(
34333439
method=response.request.method, url=response.request.url, error=error_message

tests/unit/test_blob.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4356,14 +4356,15 @@ def _call_fut(error):
43564356

43574357
return _raise_from_invalid_response(error)
43584358

4359-
def _helper(self, message, code=http_client.BAD_REQUEST, args=()):
4359+
def _helper(self, message, code=http_client.BAD_REQUEST, reason=None, args=()):
43604360
import requests
43614361

43624362
from google.resumable_media import InvalidResponse
43634363
from google.api_core import exceptions
43644364

43654365
response = requests.Response()
43664366
response.request = requests.Request("GET", "http://example.com").prepare()
4367+
response._content = reason
43674368
response.status_code = code
43684369
error = InvalidResponse(response, message, *args)
43694370

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

43824383
def test_w_206_and_args(self):
43834384
message = "Failure"
4385+
reason = b"Not available"
43844386
args = ("one", "two")
4385-
exc_info = self._helper(message, code=http_client.PARTIAL_CONTENT, args=args)
4386-
expected = "GET http://example.com/: {}".format((message,) + args)
4387+
exc_info = self._helper(
4388+
message, code=http_client.PARTIAL_CONTENT, reason=reason, args=args
4389+
)
4390+
expected = "GET http://example.com/: {}: {}".format(
4391+
reason.decode("utf-8"), (message,) + args
4392+
)
43874393
self.assertEqual(exc_info.exception.message, expected)
43884394
self.assertEqual(exc_info.exception.errors, [])
43894395

0 commit comments

Comments
 (0)