Repository: googleapis/python-storage

Issue:
Except with error messages returned from the API
 **Is your feature request related to a problem? Please describe.**

Currently the implementation will raise an exception if the answer is not 200. https://github.com/googleapis/google-resumable-media-python/blob/master/google/resumable_media/_helpers.py#L70-L98

The current implementation is terrible for development / problem triaging in production systems because it shadows the real problem making no use of the error message explaining the issue.

 **Describe the solution you'd like**

To show the messages the API return

 **Describe alternatives you've considered**

Introspecting the stack to get the message out

Discussion:
tseaver: Nit:  please drop the `(apiname)` part of the prefix for your commit summaries.  That convention made sense in the mono-repository, but post-repo-split, it is redundant and cluttery.

Patch:
diff --git a/google/cloud/storage/blob.py b/google/cloud/storage/blob.py
index c2ff579..08a86a5 100644
--- a/google/cloud/storage/blob.py
+++ b/google/cloud/storage/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


Test patch:
diff --git a/tests/unit/test_blob.py b/tests/unit/test_blob.py
index 20e20ab..b258949 100644
--- a/tests/unit/test_blob.py
+++ b/tests/unit/test_blob.py
@@ -4356,7 +4356,7 @@ class Test__raise_from_invalid_response(unittest.TestCase):
 
         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
@@ -4364,6 +4364,7 @@ class Test__raise_from_invalid_response(unittest.TestCase):
 
         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 @@ class Test__raise_from_invalid_response(unittest.TestCase):
 
     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, [])