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.
This commit is contained in:
Damir Jelić 2019-05-02 14:49:24 +02:00
parent a996d4acd3
commit 7f8d18d857

View File

@ -523,14 +523,16 @@ class ProxyDaemon:
return web.Response(status=500, text=str(e)) return web.Response(status=500, text=str(e))
if response.status == 200: if response.status == 200:
# TODO this can fail even on a 200. try:
json_response = await response.json() json_response = await response.json()
json_response = await self.decrypt_sync(client, json_response) json_response = await self.decrypt_sync(client, json_response)
return web.Response( return web.Response(
status=response.status, status=response.status,
text=json.dumps(json_response) text=json.dumps(json_response)
) )
except (JSONDecodeError, ContentTypeError):
pass
return web.Response( return web.Response(
status=response.status, status=response.status,
@ -556,13 +558,16 @@ class ProxyDaemon:
return web.Response(status=500, text=str(e)) return web.Response(status=500, text=str(e))
if response.status == 200: if response.status == 200:
json_response = await response.json() try:
json_response = client.decrypt_messages_body(json_response) json_response = await response.json()
json_response = client.decrypt_messages_body(json_response)
return web.Response( return web.Response(
status=response.status, status=response.status,
text=json.dumps(json_response) text=json.dumps(json_response)
) )
except (JSONDecodeError, ContentTypeError):
pass
return web.Response( return web.Response(
status=response.status, status=response.status,