Skip to content

Commit

Permalink
Handle JSON decode errors in async client (#75)
Browse files Browse the repository at this point in the history
* Add JSON error handling to UIT+ responses

* Change raise exception in call() to match uit.py
  • Loading branch information
araglu authored Feb 20, 2025
1 parent 0d82509 commit d398e28
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions uit/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d398e28

Please sign in to comment.