From 778fbe76f38857409f4261f79a3b0bcfecdae935 Mon Sep 17 00:00:00 2001 From: alemidev Date: Sat, 18 Jun 2022 16:11:06 +0200 Subject: [PATCH] fix: catch timed out error in auth requests --- aiocraft/mc/auth/interface.py | 44 ++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/aiocraft/mc/auth/interface.py b/aiocraft/mc/auth/interface.py index 615c3cb..35aaabc 100644 --- a/aiocraft/mc/auth/interface.py +++ b/aiocraft/mc/auth/interface.py @@ -1,6 +1,6 @@ """Minecraft authentication interface""" import logging -from multiprocessing.sharedctypes import Value +from asyncio.exceptions import TimeoutError from typing import Optional, Dict, Any import aiohttp @@ -66,28 +66,34 @@ class AuthInterface: @classmethod 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: - 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) - return data + try: + async with session.post(endpoint, **kwargs) as res: + try: + data = await res.json(content_type=None) + except JSONDecodeError: + raise AuthException(endpoint, res.status, {"error": "Invalid JSON response", "response": 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) + return data + except TimeoutError: + raise AuthException(endpoint, res.status, {"error": "request timed out"}, kwargs) @classmethod 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: - 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) - return data + try: + async with session.get(endpoint, **kwargs) as res: + try: + data = await res.json(content_type=None) + except JSONDecodeError: + raise AuthException(endpoint, res.status, {"error": "Invalid JSON response", "response": 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) + return data + except TimeoutError: + raise AuthException(endpoint, res.status, {"error": "request timed out"}, kwargs) class OfflineAuthenticator(AuthInterface): def __init__(self, name:str, id:str=''):