From 4d4ad4a6d63f74b3bc07853c5f84e7119263a05b Mon Sep 17 00:00:00 2001 From: alemidev Date: Tue, 31 May 2022 23:00:36 +0200 Subject: [PATCH] fix: catch and handle JSONDecodeError --- aiocraft/mc/auth/interface.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/aiocraft/mc/auth/interface.py b/aiocraft/mc/auth/interface.py index ed1b9b0..615c3cb 100644 --- a/aiocraft/mc/auth/interface.py +++ b/aiocraft/mc/auth/interface.py @@ -4,6 +4,7 @@ from multiprocessing.sharedctypes import Value from typing import Optional, Dict, Any import aiohttp +from json.decoder import JSONDecodeError from ..definitions import GameProfile @@ -66,7 +67,10 @@ class AuthInterface: async def _post(cls, endpoint:str, **kwargs) -> Dict[str, Any]: async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session: async with session.post(endpoint, **kwargs) as res: - data = await res.json(content_type=None) + try: + data = await res.json(content_type=None) + except JSONDecodeError: + raise AuthException(endpoint, res.status, {"invalid": await res.text()}, kwargs) logger.debug("POST /%s [%s] : %s", endpoint, str(kwargs), str(data)) if res.status >= 400: raise AuthException(endpoint, res.status, data, kwargs) @@ -76,7 +80,10 @@ class AuthInterface: async def _get(cls, endpoint:str, **kwargs) -> Dict[str, Any]: async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session: async with session.get(endpoint, **kwargs) as res: - data = await res.json(content_type=None) + try: + data = await res.json(content_type=None) + except JSONDecodeError: + raise AuthException(endpoint, res.status, {"invalid": await res.text()}, kwargs) logger.debug("GET /%s [%s] : %s", endpoint, str(kwargs), str(data)) if res.status >= 400: raise AuthException(endpoint, res.status, data, kwargs)