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

Using correct format of exceptions from JSON based APIs. #767

Merged
merged 1 commit into from
Mar 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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