From 8a8b0c113d2046a507d1a84b5c290e575b79cc3b Mon Sep 17 00:00:00 2001 From: StrayLightning Date: Tue, 5 Dec 2017 17:38:19 +0000 Subject: [PATCH] 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. --- pymisp/api.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pymisp/api.py b/pymisp/api.py index 948200e..fa2492a 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -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}