Skip to content
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
9 changes: 7 additions & 2 deletions gcloud/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,14 @@ def make_exception(response, content, error_info=None, use_json=True):
content = content.decode('utf-8')

if isinstance(content, six.string_types):
payload = None
if use_json:
payload = json.loads(content)
else:
try:
payload = json.loads(content)
except ValueError:
# Expected JSON but received something else.
pass
if payload is None:
payload = {'error': {'message': content}}
else:
payload = content
Expand Down
14 changes: 12 additions & 2 deletions gcloud/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ def test_ctor_explicit(self):

class Test_make_exception(unittest2.TestCase):

def _callFUT(self, response, content):
def _callFUT(self, response, content, error_info=None, use_json=True):
from gcloud.exceptions import make_exception
return make_exception(response, content)
return make_exception(response, content, error_info=error_info,
use_json=use_json)

def test_hit_w_content_as_str(self):
from gcloud.exceptions import NotFound
Expand All @@ -77,6 +78,15 @@ def test_miss_w_content_as_dict(self):
self.assertEqual(exception.message, 'Unknown Error')
self.assertEqual(list(exception.errors), [ERROR])

def test_html_when_json_expected(self):
from gcloud.exceptions import NotFound
response = _Response(NotFound.code)
content = '<html><body>404 Not Found</body></html>'
exception = self._callFUT(response, content, use_json=True)
self.assertTrue(isinstance(exception, NotFound))
self.assertEqual(exception.message, content)
self.assertEqual(list(exception.errors), [])


class _Response(object):
def __init__(self, status):
Expand Down