diff --git a/gcloud/exceptions.py b/gcloud/exceptions.py index a2fe8ae6a3f5..dc5458eedccb 100644 --- a/gcloud/exceptions.py +++ b/gcloud/exceptions.py @@ -179,11 +179,11 @@ def make_exception(response, content, use_json=True): if use_json: payload = json.loads(content) else: - payload = {'message': content} + payload = {'error': {'message': content}} else: payload = content - message = payload.get('message', '') + message = payload.get('error', {}).get('message', '') errors = payload.get('error', {}).get('errors', ()) try: diff --git a/gcloud/storage/batch.py b/gcloud/storage/batch.py index d7d4e3a71a28..62fdb6ac18e6 100644 --- a/gcloud/storage/batch.py +++ b/gcloud/storage/batch.py @@ -167,9 +167,11 @@ def __exit__(self, exc_type, exc_val, exc_tb): _BATCHES.pop() -def _unpack_batch_response(response, content): - """Convert response, content -> [(status, reason, payload)].""" - parser = Parser() +def _generate_faux_mime_message(parser, response, content): + """Convert response, content -> (multipart) email.message. + + Helper for _unpack_batch_response. + """ # We coerce to bytes to get consitent concat across # Py2 and Py3. Percent formatting is insufficient since # it includes the b in Py3. @@ -186,9 +188,15 @@ def _unpack_batch_response(response, content): ]) if six.PY2: - message = parser.parsestr(faux_message) + return parser.parsestr(faux_message) else: # pragma: NO COVER Python3 - message = parser.parsestr(faux_message.decode('utf-8')) + return parser.parsestr(faux_message.decode('utf-8')) + + +def _unpack_batch_response(response, content): + """Convert response, content -> [(status, reason, payload)].""" + parser = Parser() + message = _generate_faux_mime_message(parser, response, content) if not isinstance(message._payload, list): raise ValueError('Bad response: not multi-part') diff --git a/gcloud/test_exceptions.py b/gcloud/test_exceptions.py index b2d0ec2f7b0b..d42f8ebef900 100644 --- a/gcloud/test_exceptions.py +++ b/gcloud/test_exceptions.py @@ -55,7 +55,7 @@ def _callFUT(self, response, content): def test_hit_w_content_as_str(self): from gcloud.exceptions import NotFound response = _Response(404) - content = b'{"message": "Not Found"}' + content = b'{"error": {"message": "Not Found"}}' exception = self._callFUT(response, content) self.assertTrue(isinstance(exception, NotFound)) self.assertEqual(exception.message, 'Not Found') @@ -71,7 +71,7 @@ def test_miss_w_content_as_dict(self): 'reason': 'test', } response = _Response(600) - content = {"message": "Unknown Error", "error": {"errors": [ERROR]}} + content = {"error": {"message": "Unknown Error", "errors": [ERROR]}} exception = self._callFUT(response, content) self.assertTrue(isinstance(exception, GCloudError)) self.assertEqual(exception.message, 'Unknown Error')