fix: catch and handle JSONDecodeError

This commit is contained in:
əlemi 2022-05-31 23:00:36 +02:00
parent c59b11a950
commit 4d4ad4a6d6
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -4,6 +4,7 @@ from multiprocessing.sharedctypes import Value
from typing import Optional, Dict, Any from typing import Optional, Dict, Any
import aiohttp import aiohttp
from json.decoder import JSONDecodeError
from ..definitions import GameProfile from ..definitions import GameProfile
@ -66,7 +67,10 @@ class AuthInterface:
async def _post(cls, endpoint:str, **kwargs) -> Dict[str, Any]: async def _post(cls, endpoint:str, **kwargs) -> Dict[str, Any]:
async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session: async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session:
async with session.post(endpoint, **kwargs) as res: 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)) logger.debug("POST /%s [%s] : %s", endpoint, str(kwargs), str(data))
if res.status >= 400: if res.status >= 400:
raise AuthException(endpoint, res.status, data, kwargs) raise AuthException(endpoint, res.status, data, kwargs)
@ -76,7 +80,10 @@ class AuthInterface:
async def _get(cls, endpoint:str, **kwargs) -> Dict[str, Any]: async def _get(cls, endpoint:str, **kwargs) -> Dict[str, Any]:
async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session: async with aiohttp.ClientSession(timeout=cls.TIMEOUT) as session:
async with session.get(endpoint, **kwargs) as res: 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)) logger.debug("GET /%s [%s] : %s", endpoint, str(kwargs), str(data))
if res.status >= 400: if res.status >= 400:
raise AuthException(endpoint, res.status, data, kwargs) raise AuthException(endpoint, res.status, data, kwargs)