rewrite exceptions

This commit is contained in:
Victor Kotlin 2021-09-11 15:27:11 +03:00
parent aca61cbdc7
commit 1076ffed5b
4 changed files with 11 additions and 17 deletions

View file

@ -15,17 +15,10 @@ class HTTPException(BoticordException):
The text of the error. Could be an empty string. The text of the error. Could be an empty string.
""" """
def __init__(self, response, message): def __init__(self, response):
self.response = response self.response = response
if isinstance(message, dict):
self.text = message.get('message', '')
self.code = message.get('code', 0)
else:
self.text = message
fmt = f"{self.response.reason} (Status code: {self.response.status})" fmt = f"{self.response.reason} (Status code: {self.response.status})"
if self.text:
fmt = f"{fmt}: {self.text}"
super().__init__(fmt) super().__init__(fmt)

View file

@ -46,8 +46,9 @@ class Bots:
async with self.session.get(f'{Config.general_api}/bot/{botID}', headers=headers) as resp: async with self.session.get(f'{Config.general_api}/bot/{botID}', headers=headers) as resp:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data
async def getBotComments(self, botID: int): async def getBotComments(self, botID: int):
@ -65,7 +66,7 @@ class Bots:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data
async def postStats(self, stats: dict): async def postStats(self, stats: dict):
@ -86,5 +87,5 @@ class Bots:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data

View file

@ -48,7 +48,7 @@ class Servers:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data
async def getServerComments(self, serverID: int): async def getServerComments(self, serverID: int):
@ -66,7 +66,7 @@ class Servers:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data
async def postServerStats(self, message: discord.Message, custom_stats: dict = None): async def postServerStats(self, message: discord.Message, custom_stats: dict = None):
@ -107,5 +107,5 @@ class Servers:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data

View file

@ -41,7 +41,7 @@ class Users:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data
async def getUserComments(self, userID: int): async def getUserComments(self, userID: int):
@ -59,7 +59,7 @@ class Users:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data
async def getUserBots(self, userID: int): async def getUserBots(self, userID: int):
@ -77,5 +77,5 @@ class Users:
data = await _json_or_text(resp) data = await _json_or_text(resp)
status = Config.http_exceptions.get(resp.status) status = Config.http_exceptions.get(resp.status)
if status is not None: if status is not None:
raise status raise status(resp)
return data return data