diff options
author | 2021-08-08 15:51:44 +0200 | |
---|---|---|
committer | 2021-08-08 15:51:44 +0200 | |
commit | ff0d3a833bbbe517f50124c5157223ff95876931 (patch) | |
tree | a857bc79dcc672d01a907eab7381b25759c872fb | |
parent | Resolve merge conflict (diff) |
Improve code consitency in Wikipedia Cog,Exceptions, and Error handler
-rw-r--r-- | bot/exts/evergreen/error_handler.py | 11 | ||||
-rw-r--r-- | bot/exts/evergreen/wikipedia.py | 15 | ||||
-rw-r--r-- | bot/utils/exceptions.py | 9 |
3 files changed, 20 insertions, 15 deletions
diff --git a/bot/exts/evergreen/error_handler.py b/bot/exts/evergreen/error_handler.py index 1662af50..8fcbb8f2 100644 --- a/bot/exts/evergreen/error_handler.py +++ b/bot/exts/evergreen/error_handler.py @@ -113,7 +113,10 @@ class CommandErrorHandler(commands.Cog): return if isinstance(error, commands.CheckFailure): - await ctx.send(embed=self.error_embed("You are not authorized to use this command.", NEGATIVE_REPLIES)) + await ctx.send( + embed=self.error_embed( + "You are not authorized to use this command.", + NEGATIVE_REPLIES)) return if isinstance(error, UserNotPlayingError): @@ -121,8 +124,10 @@ class CommandErrorHandler(commands.Cog): return if isinstance(error, ExternalAPIError): - await ctx.send(embed=self.error_embed(f"There was an error when communicating with the {error.api}", - NEGATIVE_REPLIES)) + await ctx.send( + embed=self.error_embed( + f"There was an error when communicating with the {error.api}", + NEGATIVE_REPLIES)) return with push_scope() as scope: diff --git a/bot/exts/evergreen/wikipedia.py b/bot/exts/evergreen/wikipedia.py index 879146a6..63462433 100644 --- a/bot/exts/evergreen/wikipedia.py +++ b/bot/exts/evergreen/wikipedia.py @@ -47,9 +47,9 @@ class WikipediaSearch(commands.Cog): """Search wikipedia search string and return formatted first 10 pages found.""" params = WIKI_PARAMS | {"srlimit": 10, "srsearch": search} async with self.bot.http_session.get(url=SEARCH_API, params=params) as resp: - if resp.status != 200: + if (status := resp.status) != 200: log.info(f"Unexpected response `{resp.status}` while searching wikipedia for `{search}`") - raise ExternalAPIError("Wikipedia API") + raise ExternalAPIError("Wikipedia API", status) raw_data = await resp.json() number_of_results = raw_data["query"]["searchinfo"]["totalhits"] @@ -72,13 +72,10 @@ class WikipediaSearch(commands.Cog): return lines raw_data = await resp.json() - if raw_data.get("query", None) is None: - if raw_data.get("errors", None) is not None: - log.info("There was an error regarding the Wikipedia API query.") - else: - log.info("There was an issue when trying to communicate with the Wikipedia API, " - "please try again later.") - raise ExternalAPIError("Wikipedia API") + if not raw_data.get("query", None): + if error := raw_data.get("errors"): + log.error(f"There was an error while communicating with the Wikipedia API: {error}") + raise ExternalAPIError("Wikipedia API", status, error) number_of_results = raw_data["query"]["searchinfo"]["totalhits"] diff --git a/bot/utils/exceptions.py b/bot/utils/exceptions.py index 5ccd6139..73c8e3e1 100644 --- a/bot/utils/exceptions.py +++ b/bot/utils/exceptions.py @@ -1,3 +1,5 @@ +from typing import Optional + class UserNotPlayingError(Exception): """Raised when users try to use game commands when they are not playing.""" @@ -6,9 +8,10 @@ class UserNotPlayingError(Exception): class ExternalAPIError(Exception): - """Raised when an external API(eg. Wikipedia) returns an error.""" + """Raised when an external API(eg. Wikipedia) returns an error response.""" - def __init__(self, api: str): + def __init__(self, api: str, status_code: int, error_msg: Optional[str] = None): super().__init__() self.api = api - pass + self.status_code = status_code + self.error_msg = error_msg |