From 8a8b0c113d2046a507d1a84b5c290e575b79cc3b Mon Sep 17 00:00:00 2001 From: StrayLightning Date: Tue, 5 Dec 2017 17:38:19 +0000 Subject: [PATCH 1/2] 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} From 748be6a0939a109df27e43e84185ea4e6a22c2d6 Mon Sep 17 00:00:00 2001 From: StrayLightning Date: Tue, 5 Dec 2017 18:04:11 +0000 Subject: [PATCH 2/2] Improve the exception message for a server 500+ response with no response content --- pymisp/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pymisp/api.py b/pymisp/api.py index fa2492a..9806f1d 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -224,7 +224,7 @@ class PyMISP(object): errors = [] if response.status_code >= 500: if len(response.content) == 0: - raise PyMISPError('Something bad happened on the server-side and there was no content to be decoded') + raise PyMISPError('Something bad happened on the server-side, but there was no response content to be decoded') else: errors.append(response.json()) logger.critical('Something bad happened on the server-side: {}'.format(response.json()))