diff options
| author | 2019-08-05 21:11:14 +0200 | |
|---|---|---|
| committer | 2019-08-05 21:11:14 +0200 | |
| commit | 0e73db0aab6845956bf40493a93c07c4d81e81fe (patch) | |
| tree | ed1520f79d49c0b5b29819d9bfd8e3fdd5e231f3 | |
| parent | Update code to make use of the new `ResponseCodeError`. (diff) | |
Finalize error handling.
| -rw-r--r-- | bot/api.py | 8 | ||||
| -rw-r--r-- | bot/cogs/error_handler.py | 13 | ||||
| -rw-r--r-- | bot/cogs/sync/cog.py | 4 | ||||
| -rw-r--r-- | bot/cogs/watchchannels/talentpool.py | 2 |
4 files changed, 15 insertions, 12 deletions
diff --git a/bot/api.py b/bot/api.py index 935ff699f..e926a262e 100644 --- a/bot/api.py +++ b/bot/api.py @@ -1,4 +1,3 @@ -import typing from urllib.parse import quote as quote_url import aiohttp @@ -6,8 +5,9 @@ import aiohttp from .constants import Keys, URLs -class ResponseCodeError(typing.NamedTuple, ValueError): - response: aiohttp.ClientResponse +class ResponseCodeError(ValueError): + def __init__(self, response: aiohttp.ClientResponse): + self.response = response class APIClient: @@ -28,7 +28,7 @@ class APIClient: return f"{URLs.site_schema}{URLs.site_api}/{quote_url(endpoint)}" def maybe_raise_for_status(self, response: aiohttp.ClientResponse, should_raise: bool): - if should_raise and response.status_code >= 400: + if should_raise and response.status >= 400: raise ResponseCodeError(response=response) async def get(self, endpoint: str, *args, raise_for_status: bool = True, **kwargs): diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index b6ca7fccf..25aa177e1 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -1,6 +1,5 @@ import logging -from discord.ext.commands import Bot, Context from discord.ext.commands import ( BadArgument, BotMissingPermissions, @@ -10,6 +9,7 @@ from discord.ext.commands import ( NoPrivateMessage, UserInputError, ) +from discord.ext.commands import Bot, Context from bot.api import ResponseCodeError @@ -61,11 +61,13 @@ class ErrorHandler: ) elif isinstance(e, CommandInvokeError): if isinstance(e.original, ResponseCodeError): - if e.original.response.status_code == 404: + if e.original.response.status == 404: await ctx.send("There does not seem to be anything matching your query.") - elif e.original.response.status_code == 400: + elif e.original.response.status == 400: + content = await e.original.resopnse.json() + log.debug("API gave bad request on command. Response: %r.", content) await ctx.send("According to the API, your request is malformed.") - elif 500 <= e.original.response.status_code < 600: + elif 500 <= e.original.response.status < 600: await ctx.send("Sorry, there seems to be an internal issue with the API.") else: await ctx.send( @@ -78,7 +80,8 @@ class ErrorHandler: f"Sorry, an unexpected error occurred. Please let us know!\n\n```{e}```" ) raise e.original - raise e + else: + raise e def setup(bot: Bot): diff --git a/bot/cogs/sync/cog.py b/bot/cogs/sync/cog.py index 9e71f749d..222c1668b 100644 --- a/bot/cogs/sync/cog.py +++ b/bot/cogs/sync/cog.py @@ -96,7 +96,7 @@ class Sync: except ResponseCodeError as e: # If we didn't get 404, something else broke - propagate it up. - if e.response.status_code != 404: + if e.response.status != 404: raise got_error = True # yikes @@ -138,7 +138,7 @@ class Sync: } ) except ResponseCodeError as e: - if e.response.status_code != 404: + if e.response.status != 404: raise log.warning( diff --git a/bot/cogs/watchchannels/talentpool.py b/bot/cogs/watchchannels/talentpool.py index 44bf6371b..47d207d05 100644 --- a/bot/cogs/watchchannels/talentpool.py +++ b/bot/cogs/watchchannels/talentpool.py @@ -171,7 +171,7 @@ class TalentPool(WatchChannel): try: nomination = await self.bot.api_client.get(f"{self.api_endpoint}/{nomination_id}") except ResponseCodeError as e: - if e.response.status_code == 404: + if e.response.status == 404: self.log.trace(f"Nomination API 404: Can't nomination with id {nomination_id}") await ctx.send(f":x: Can't find a nomination with id `{nomination_id}`") return |