better exception

This commit is contained in:
əlemi 2021-12-15 13:05:47 +01:00
parent 89635a4c28
commit 12878d856d

View file

@ -9,13 +9,15 @@ from typing import Optional
import aiohttp import aiohttp
class AuthException(Exception): class AuthException(Exception):
pass action : str
type : str
message : str
def _raise_from_json(endpoint:str, data:dict): def __init__(self, action:str, data:dict):
err_type = data["error"] if data and "error" in data else "Unknown Error" self.type = data["error"] if data and "error" in data else "Unknown"
err_msg = data["errorMessage"] if data and "errorMessage" in data else "Credentials invalid or token not refreshable anymore" self.message = data["errorMessage"] if data and "errorMessage" in data else "Token or credentials invalid"
action = endpoint.rsplit('/',1)[1] self.action = action.rsplit('/',1)[1]
raise AuthException(f"[{action}] {err_type} : {err_msg}") super().__init__(f"[{self.action}] {self.type} : {self.message}")
@dataclass @dataclass
class GameProfile: class GameProfile:
@ -157,7 +159,7 @@ class Token:
async with sess.post(endpoint, headers=cls.HEADERS, json=data) as res: async with sess.post(endpoint, headers=cls.HEADERS, json=data) as res:
data = await res.json(content_type=None) data = await res.json(content_type=None)
if res.status >= 400: if res.status >= 400:
_raise_from_json(endpoint, data) raise AuthException(endpoint, data)
return data return data
@classmethod @classmethod
@ -166,5 +168,5 @@ class Token:
async with sess.get(endpoint, headers=cls.HEADERS, params=data) as res: async with sess.get(endpoint, headers=cls.HEADERS, params=data) as res:
data = await res.json(content_type=None) data = await res.json(content_type=None)
if res.status >= 400: if res.status >= 400:
_raise_from_json(endpoint, data) raise AuthException(endpoint, data)
return data return data