Skip to content

Commit

Permalink
Merge pull request #767 from dhermes/add-back-762
Browse files Browse the repository at this point in the history
Using correct format of exceptions from JSON based APIs.
  • Loading branch information
dhermes committed Mar 28, 2015
2 parents 622d994 + 344e608 commit 4d69bb3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions gcloud/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 13 additions & 5 deletions gcloud/storage/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions gcloud/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand Down

0 comments on commit 4d69bb3

Please sign in to comment.