From ad0cd41be4f31f624ee143edc4ad098c5ea042ab Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Thu, 1 Feb 2024 19:32:11 -0500 Subject: [PATCH] fix(bible): added error handling --- bible/bible.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/bible/bible.py b/bible/bible.py index a53bc23..2a2ec7c 100644 --- a/bible/bible.py +++ b/bible/bible.py @@ -12,8 +12,9 @@ import aiohttp from discord import Embed from redbot.core import Config, commands from redbot.core.bot import Red +from redbot.core.utils.chat_formatting import error -from bible.errors import BibleAccessError, NotFound, Unauthorized +import bible.errors class Bible(commands.Cog): @@ -33,6 +34,22 @@ class Bible(commands.Cog): self.config.register_global(bible="de4e12af7f28f599-01") self.config.register_user(bible=None) + async def on_command_error(self, ctx: commands.Context, err): + if isinstance(err, bible.errors.Unauthorized): + await ctx.send( + error( + "The API key for API.bible is missing or invalid. Please report this to the bot owner." + ) + ) + if isinstance(err, bible.errors.BibleAccessError): + await ctx.send( + error( + "The provided API key cannot retrieve sections from the configured Bible. Please report this to the bot owner." + ) + ) + if isinstance(err, bible.errors.NotFound): + await ctx.send(error("The requested passage was not found.")) + async def translate_book_name(self, bible_id: str, book_name: str) -> str: """Translate a book name to a book ID.""" book_name_list = book_name.split() @@ -68,13 +85,13 @@ class Bible(commands.Cog): "_get_passage executed with a response code of: %s", response.status ) if response.status == 401: - raise Unauthorized("Missing/Invalid API key.") + raise bible.errors.Unauthorized("Missing/Invalid API key.") if response.status == 403: - raise BibleAccessError( + raise bible.errors.BibleAccessError( "The provided API key cannot retrieve sections from the configured Bible." ) if response.status == 404: - raise NotFound("The requested passage was not found.") + raise bible.errors.NotFound("The requested passage was not found.") data = await response.json() return data["data"] @@ -88,9 +105,9 @@ class Bible(commands.Cog): "_get_books executed with a response code of: %s", response.status ) if response.status == 401: - raise Unauthorized("Missing/Invalid API key.") + raise bible.errors.Unauthorized("Missing/Invalid API key.") if response.status == 403: - raise BibleAccessError( + raise bible.errors.BibleAccessError( "The provided API key cannot retrieve sections from the configured Bible." ) data = await response.json() @@ -106,9 +123,9 @@ class Bible(commands.Cog): "_get_chapters executed with a response code of: %s", response.status ) if response.status == 401: - raise Unauthorized("Missing/Invalid API key.") + raise bible.errors.Unauthorized("Missing/Invalid API key.") if response.status == 403: - raise BibleAccessError( + raise bible.errors.BibleAccessError( "The provided API key cannot retrieve sections from the configured Bible." ) data = await response.json() @@ -124,9 +141,9 @@ class Bible(commands.Cog): "_get_verses executed with a response code of: %s", response.status ) if response.status == 401: - raise Unauthorized("Missing/Invalid API key.") + raise bible.errors.Unauthorized("Missing/Invalid API key.") if response.status == 403: - raise BibleAccessError( + raise bible.errors.BibleAccessError( "The provided API key cannot retrieve sections from the configured Bible." ) data = await response.json()