aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Sebastiaan Zeeff <[email protected]>2019-09-14 14:39:58 +0200
committerGravatar Sebastiaan Zeeff <[email protected]>2019-09-14 14:39:58 +0200
commitbbbdfcec41c06eaf0d031d7f2a58f111e8e88a16 (patch)
tree434aa2e5227834a81a7b0e75be1f4a6b7db00721
parentWe now also detect bot tokens outside quotes. (diff)
Make ResponseErrorCode handle non-JSON response
The previous commit assumed the API respone to always be JSON. This leads to issues when that is not the case, such as when the API is completely unreachable (text/html 404 response). Updated the ResponseErrorCode exception to account for that and updated the moderation util `post_infraction` to reflect that.
-rw-r--r--bot/api.py22
-rw-r--r--bot/utils/moderation.py2
2 files changed, 18 insertions, 6 deletions
diff --git a/bot/api.py b/bot/api.py
index 36f9cfcd4..9a0ebaa26 100644
--- a/bot/api.py
+++ b/bot/api.py
@@ -1,5 +1,6 @@
import asyncio
import logging
+from typing import Optional
from urllib.parse import quote as quote_url
import aiohttp
@@ -10,13 +11,20 @@ log = logging.getLogger(__name__)
class ResponseCodeError(ValueError):
- def __init__(self, response: aiohttp.ClientResponse, response_data: dict):
+ def __init__(
+ self,
+ response: aiohttp.ClientResponse,
+ response_json: Optional[dict] = None,
+ response_text: str = ""
+ ):
self.status = response.status
- self.response_data = response_data
+ self.response_json = response_json or {}
+ self.response_text = response_text
self.response = response
def __str__(self):
- return f"Status: {self.status} Response: {self.response_data}"
+ response = self.response_json if self.response_json else self.response_text
+ return f"Status: {self.status} Response: {response}"
class APIClient:
@@ -38,8 +46,12 @@ class APIClient:
async def maybe_raise_for_status(self, response: aiohttp.ClientResponse, should_raise: bool):
if should_raise and response.status >= 400:
- response_data = await response.json()
- raise ResponseCodeError(response=response, response_data=response_data)
+ try:
+ response_json = await response.json()
+ raise ResponseCodeError(response=response, response_json=response_json)
+ except aiohttp.ContentTypeError:
+ response_text = await response.text()
+ raise ResponseCodeError(response=response, response_text=response_text)
async def get(self, endpoint: str, *args, raise_for_status: bool = True, **kwargs):
async with self.session.get(self._url_for(endpoint), *args, **kwargs) as resp:
diff --git a/bot/utils/moderation.py b/bot/utils/moderation.py
index b295e4649..7860f14a1 100644
--- a/bot/utils/moderation.py
+++ b/bot/utils/moderation.py
@@ -37,7 +37,7 @@ async def post_infraction(
try:
response = await ctx.bot.api_client.post('bot/infractions', json=payload)
except ResponseCodeError as exp:
- if exp.status == 400 and 'user' in exp.response_data:
+ if exp.status == 400 and 'user' in exp.response_json:
log.info(
f"{ctx.author} tried to add a {type} infraction to `{user.id}`, "
"but that user id was not found in the database."