From 7f8d18d857a9c416a2e2835db4cce7f93441b066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 2 May 2019 14:49:24 +0200 Subject: [PATCH] daemon: Handle json errors in the sync and messages methods. A valid 200 response from the server doesn't necessarily mean that the content will be valid json. Handle decode errors and return the raw body if we're unable to decode. --- pantalaimon/daemon.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/pantalaimon/daemon.py b/pantalaimon/daemon.py index 9eab525..c35c849 100755 --- a/pantalaimon/daemon.py +++ b/pantalaimon/daemon.py @@ -523,14 +523,16 @@ class ProxyDaemon: return web.Response(status=500, text=str(e)) if response.status == 200: - # TODO this can fail even on a 200. - json_response = await response.json() - json_response = await self.decrypt_sync(client, json_response) + try: + json_response = await response.json() + json_response = await self.decrypt_sync(client, json_response) - return web.Response( - status=response.status, - text=json.dumps(json_response) - ) + return web.Response( + status=response.status, + text=json.dumps(json_response) + ) + except (JSONDecodeError, ContentTypeError): + pass return web.Response( status=response.status, @@ -556,13 +558,16 @@ class ProxyDaemon: return web.Response(status=500, text=str(e)) if response.status == 200: - json_response = await response.json() - json_response = client.decrypt_messages_body(json_response) + try: + json_response = await response.json() + json_response = client.decrypt_messages_body(json_response) - return web.Response( - status=response.status, - text=json.dumps(json_response) - ) + return web.Response( + status=response.status, + text=json.dumps(json_response) + ) + except (JSONDecodeError, ContentTypeError): + pass return web.Response( status=response.status,