Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit d05e6b9

Browse files
authored
Merge pull request #260 from cloudant/65-improve-append-response-error-content
Improved logic for appending http error and message
2 parents d67db05 + 737cd30 commit d05e6b9

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/cloudant/_common_util.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -268,16 +268,24 @@ def get_docs(r_session, url, encoder=None, headers=None, **params):
268268
#pylint: disable=unused-argument
269269
def append_response_error_content(response, **kwargs):
270270
"""
271-
Provides a helper to add HTTP response error and reason messages.
271+
Provides a helper to act as callback function for the response event hook
272+
and add a HTTP response error with reason message to ``response.reason``.
273+
The ``response`` and ``**kwargs`` are necessary for this function to
274+
properly operate as the callback.
275+
276+
:param response: HTTP response object
277+
:param kwargs: HTTP request parameters
272278
"""
273-
try:
274-
if response.status_code >= 400 and response.json():
275-
reason = response.json().pop('reason', None)
276-
error = response.json().pop('error', None)
277-
response.reason += ' %s %s' % (error, reason)
278-
return response
279-
except ValueError:
280-
return
279+
if response.status_code >= 400:
280+
try:
281+
resp_dict = response.json()
282+
error = resp_dict.get('error', '')
283+
reason = resp_dict.get('reason', '')
284+
# Append to the existing response's reason
285+
response.reason += ' {0} {1}'.format(error, reason)
286+
except ValueError:
287+
pass
288+
return response
281289

282290
# Classes
283291

0 commit comments

Comments
 (0)