From d398e28a805d91da3977bbe5205ebf7d96033e42 Mon Sep 17 00:00:00 2001 From: Mark Lugar <14322382+araglu@users.noreply.github.com> Date: Thu, 20 Feb 2025 14:30:47 -0600 Subject: [PATCH] Handle JSON decode errors in async client (#75) * Add JSON error handling to UIT+ responses * Change raise exception in call() to match uit.py --- uit/async_client.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/uit/async_client.py b/uit/async_client.py index 4606f73..da74389 100644 --- a/uit/async_client.py +++ b/uit/async_client.py @@ -280,7 +280,16 @@ async def call( else: return "ERROR! Gateway Timeout" - resp = await r.json() + try: + resp = await r.json() + except aiohttp.client_exceptions.ContentTypeError as e: + logger.error( + "JSON Parse Error '%s' - Status code: %s Content: %s", + str(e), + r.status, + await r.text(), + ) + raise if full_response: return resp @@ -328,7 +337,17 @@ async def put_file(self, local_path, remote_path=None, timeout=30): raise UITError("Request Timeout") logger.debug(await self._debug_uit(locals())) - return await r.json() + try: + return await r.json() + except aiohttp.client_exceptions.ContentTypeError as e: + # UIT should always return JSON, but other services may return an HTML error + logger.error( + "JSON Parse Error '%s' - Status code: %s Content: %s", + str(e), + r.status, + await r.text(), + ) + raise UITError("Upload error") from e @_ensure_connected @robust()