aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Johannes Christ <[email protected]>2019-08-05 20:36:20 +0200
committerGravatar Johannes Christ <[email protected]>2019-08-05 20:36:20 +0200
commit7cb8a8181cf721c4b7539faca054994ce76a4685 (patch)
tree3c8df563feb38b8bac0870d57af647a1c2e0644d
parentRaise specific exception for non-200s. (diff)
Update code to make use of the new `ResponseCodeError`.
-rw-r--r--bot/api.py2
-rw-r--r--bot/cogs/error_handler.py16
-rw-r--r--bot/cogs/sync/cog.py10
-rw-r--r--bot/cogs/watchchannels/talentpool.py6
-rw-r--r--bot/cogs/watchchannels/watchchannel.py6
5 files changed, 22 insertions, 18 deletions
diff --git a/bot/api.py b/bot/api.py
index 4f4ffeff3..935ff699f 100644
--- a/bot/api.py
+++ b/bot/api.py
@@ -41,7 +41,7 @@ class APIClient:
self.maybe_raise_for_status(resp, raise_for_status)
return await resp.json()
- async def post(self, endpoint: str, *args, raise_for_status: bool = True,**kwargs):
+ async def post(self, endpoint: str, *args, raise_for_status: bool = True, **kwargs):
async with self.session.post(self._url_for(endpoint), *args, **kwargs) as resp:
self.maybe_raise_for_status(resp, raise_for_status)
return await resp.json()
diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py
index 0bb2faf43..b6ca7fccf 100644
--- a/bot/cogs/error_handler.py
+++ b/bot/cogs/error_handler.py
@@ -1,6 +1,5 @@
import logging
-from aiohttp import ClientResponseError
from discord.ext.commands import Bot, Context
from discord.ext.commands import (
BadArgument,
@@ -12,6 +11,8 @@ from discord.ext.commands import (
UserInputError,
)
+from bot.api import ResponseCodeError
+
log = logging.getLogger(__name__)
@@ -59,15 +60,18 @@ class ErrorHandler:
f"Here's what I'm missing: **{e.missing_perms}**"
)
elif isinstance(e, CommandInvokeError):
- if isinstance(e.original, ClientResponseError):
- if e.original.code == 404:
+ if isinstance(e.original, ResponseCodeError):
+ if e.original.response.status_code == 404:
await ctx.send("There does not seem to be anything matching your query.")
- elif e.original.code == 400:
+ elif e.original.response.status_code == 400:
await ctx.send("According to the API, your request is malformed.")
- elif 500 <= e.original.code < 600:
+ elif 500 <= e.original.response.status_code < 600:
await ctx.send("Sorry, there seems to be an internal issue with the API.")
else:
- await ctx.send(f"Got an unexpected status code from the API (`{e.original.code}`).")
+ await ctx.send(
+ "Got an unexpected status code from the "
+ f"API (`{e.original.response.code}`)."
+ )
else:
await ctx.send(
diff --git a/bot/cogs/sync/cog.py b/bot/cogs/sync/cog.py
index ab591ebf8..9e71f749d 100644
--- a/bot/cogs/sync/cog.py
+++ b/bot/cogs/sync/cog.py
@@ -1,12 +1,12 @@
import logging
from typing import Callable, Iterable
-import aiohttp
from discord import Guild, Member, Role
from discord.ext import commands
from discord.ext.commands import Bot
from bot import constants
+from bot.api import ResponseCodeError
from bot.cogs.sync import syncers
log = logging.getLogger(__name__)
@@ -94,9 +94,9 @@ class Sync:
# fields that may have changed since the last time we've seen them.
await self.bot.api_client.put('bot/users/' + str(member.id), json=packed)
- except aiohttp.client_exceptions.ClientResponseError as e:
+ except ResponseCodeError as e:
# If we didn't get 404, something else broke - propagate it up.
- if e.status != 404:
+ if e.response.status_code != 404:
raise
got_error = True # yikes
@@ -137,8 +137,8 @@ class Sync:
'roles': sorted(role.id for role in after.roles)
}
)
- except aiohttp.client_exceptions.ClientResponseError as e:
- if e.status != 404:
+ except ResponseCodeError as e:
+ if e.response.status_code != 404:
raise
log.warning(
diff --git a/bot/cogs/watchchannels/talentpool.py b/bot/cogs/watchchannels/talentpool.py
index 6fbe2bc03..44bf6371b 100644
--- a/bot/cogs/watchchannels/talentpool.py
+++ b/bot/cogs/watchchannels/talentpool.py
@@ -3,10 +3,10 @@ import textwrap
from collections import ChainMap
from typing import Union
-from aiohttp.client_exceptions import ClientResponseError
from discord import Color, Embed, Member, User
from discord.ext.commands import Context, group
+from bot.api import ResponseCodeError
from bot.constants import Channels, Guild, Roles, Webhooks
from bot.decorators import with_role
from bot.pagination import LinePaginator
@@ -170,8 +170,8 @@ class TalentPool(WatchChannel):
"""
try:
nomination = await self.bot.api_client.get(f"{self.api_endpoint}/{nomination_id}")
- except ClientResponseError as e:
- if e.status == 404:
+ except ResponseCodeError as e:
+ if e.response.status_code == 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
diff --git a/bot/cogs/watchchannels/watchchannel.py b/bot/cogs/watchchannels/watchchannel.py
index fe6d6bb6e..3a24e3f21 100644
--- a/bot/cogs/watchchannels/watchchannel.py
+++ b/bot/cogs/watchchannels/watchchannel.py
@@ -8,11 +8,11 @@ from collections import defaultdict, deque
from dataclasses import dataclass
from typing import Optional
-import aiohttp
import discord
from discord import Color, Embed, Message, Object, errors
from discord.ext.commands import BadArgument, Bot, Context
+from bot.api import ResponseCodeError
from bot.cogs.modlog import ModLog
from bot.constants import BigBrother as BigBrotherConfig, Guild as GuildConfig, Icons
from bot.pagination import LinePaginator
@@ -157,8 +157,8 @@ class WatchChannel(ABC):
"""
try:
data = await self.bot.api_client.get(self.api_endpoint, params=self.api_default_params)
- except aiohttp.ClientResponseError as e:
- self.log.exception(f"Failed to fetch the watched users from the API", exc_info=e)
+ except ResponseCodeError as err:
+ self.log.exception(f"Failed to fetch the watched users from the API", exc_info=err)
return False
self.watched_users = defaultdict(dict)