8000 Merge pull request #628 from dhermes/fix-626 · googleapis/google-cloud-python@bdc591e · GitHub
[go: up one dir, main page]

Skip to conte 10000 nt

Commit bdc591e

Browse files
committed
Merge pull request #628 from dhermes/fix-626
Making json.loads() optional in gcloud.exceptions.make_exception.
2 parents f73ad7e + c3e1351 commit bdc591e

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

gcloud/datastore/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def _request(self, dataset_id, method, data):
6767

6868
status = headers['status']
6969
if status != '200':
70-
raise make_exception(headers, content)
70+
raise make_exception(headers, content, use_json=False)
7171

7272
return content
7373

gcloud/datastore/test_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ def test__request_not_200(self):
106106
METHOD = 'METHOD'
107107
DATA = 'DATA'
108108
conn = self._makeOne()
109-
conn._http = Http({'status': '400'}, '{"message": "Bad Request"}')
109+
conn._http = Http({'status': '400'}, 'Entity value is indexed.')
110110
with self.assertRaises(BadRequest) as e:
111111
conn._request(DATASET_ID, METHOD, DATA)
112-
expected_message = ('400 Bad Request')
112+
expected_message = '400 Entity value is indexed.'
113113
self.assertEqual(str(e.exception), expected_message)
114114

115115
def test__rpc(self):

gcloud/exceptions.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,18 +155,35 @@ class ServiceUnavailable(ServerError):
155155
code = 503
156156

157157

158-
def make_exception(response, content):
158+
def make_exception(response, content, use_json=True):
159159
"""Factory: create exception based on HTTP response code.
160160
161+
:type response: :class:`httplib2.Response` or other HTTP response object
162+
:param response: A response object that defines a status code as the
163+
status attribute.
164+
165+
:type content: string or dictionary
166+
:param content: The body of the HTTP error response.
167+
168+
:type use_json: boolean
169+
:param use_json: Flag indicating if ``content`` is expected to be JSON.
170+
161171
:rtype: instance of :class:`GCloudError`, or a concrete subclass.
172+
:returns: Exception specific to the error response.
162173
"""
174+
message = content
175+
errors = ()
163176

164177
if isinstance(content, str):
165-
content = json.loads(content)
178+
if use_json:
179+
payload = json.loads(content)
180+
else:
181+
payload = {}
182+
else:
183+
payload = content
166184

167-
message = content.get('message')
168-
error = content.get('error', {})
169-
errors = error.get('errors', ())
185+
message = payload.get('message', message)
186+
errors = payload.get('error', {}).get('errors', ())
170187

171188
try:
172189
klass = _HTTP_CODE_TO_EXCEPTION[response.status]

0 commit comments

Comments
 (0)
0