Check for zero-length 500 response from the server and produce a suitable error message

In experimenting with PyMISP I am triggering problems on the server I
am using.  Occasionally the server will return a 500 response with a
message indicating an internal error, but more often than not it returns
a 500 response with no contents, and _check_response falls over itself,
generating hard-to-fathom exception from the json internals.

This commit hardens _check_response by detecting zero-length responses
and raising a suitable exception.

Also fix a missing bracket in one of the subsequent exception strings.
pull/147/head
StrayLightning 2017-12-05 17:38:19 +00:00
parent 1d190cdf50
commit 8a8b0c113d
1 changed files with 6 additions and 3 deletions

View File

@ -223,13 +223,16 @@ class PyMISP(object):
"""Check if the response from the server is not an unexpected error"""
errors = []
if response.status_code >= 500:
errors.append(response.json())
logger.critical('Something bad happened on the server-side: {}'.format(response.json()))
if len(response.content) == 0:
raise PyMISPError('Something bad happened on the server-side and there was no content to be decoded')
else:
errors.append(response.json())
logger.critical('Something bad happened on the server-side: {}'.format(response.json()))
try:
to_return = response.json()
except ValueError:
# It the server didn't return a JSON blob, we've a problem.
raise PyMISPError('Unknown error (something is very broken server-side: {}'.format(response.text))
raise PyMISPError('Unknown error (something is very broken server-side: {})'.format(response.text))
if isinstance(to_return, (list, str)):
to_return = {'response': to_return}