fix(bible): added error handling

This commit is contained in:
Seaswimmer 2024-02-01 19:32:11 -05:00
parent 8da6f0d8de
commit ad0cd41be4
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -12,8 +12,9 @@ import aiohttp
from discord import Embed from discord import Embed
from redbot.core import Config, commands from redbot.core import Config, commands
from redbot.core.bot import Red 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): class Bible(commands.Cog):
@ -33,6 +34,22 @@ class Bible(commands.Cog):
self.config.register_global(bible="de4e12af7f28f599-01") self.config.register_global(bible="de4e12af7f28f599-01")
self.config.register_user(bible=None) 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: async def translate_book_name(self, bible_id: str, book_name: str) -> str:
"""Translate a book name to a book ID.""" """Translate a book name to a book ID."""
book_name_list = book_name.split() 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 "_get_passage executed with a response code of: %s", response.status
) )
if response.status == 401: if response.status == 401:
raise Unauthorized("Missing/Invalid API key.") raise bible.errors.Unauthorized("Missing/Invalid API key.")
if response.status == 403: if response.status == 403:
raise BibleAccessError( raise bible.errors.BibleAccessError(
"The provided API key cannot retrieve sections from the configured Bible." "The provided API key cannot retrieve sections from the configured Bible."
) )
if response.status == 404: 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() data = await response.json()
return data["data"] return data["data"]
@ -88,9 +105,9 @@ class Bible(commands.Cog):
"_get_books executed with a response code of: %s", response.status "_get_books executed with a response code of: %s", response.status
) )
if response.status == 401: if response.status == 401:
raise Unauthorized("Missing/Invalid API key.") raise bible.errors.Unauthorized("Missing/Invalid API key.")
if response.status == 403: if response.status == 403:
raise BibleAccessError( raise bible.errors.BibleAccessError(
"The provided API key cannot retrieve sections from the configured Bible." "The provided API key cannot retrieve sections from the configured Bible."
) )
data = await response.json() data = await response.json()
@ -106,9 +123,9 @@ class Bible(commands.Cog):
"_get_chapters executed with a response code of: %s", response.status "_get_chapters executed with a response code of: %s", response.status
) )
if response.status == 401: if response.status == 401:
raise Unauthorized("Missing/Invalid API key.") raise bible.errors.Unauthorized("Missing/Invalid API key.")
if response.status == 403: if response.status == 403:
raise BibleAccessError( raise bible.errors.BibleAccessError(
"The provided API key cannot retrieve sections from the configured Bible." "The provided API key cannot retrieve sections from the configured Bible."
) )
data = await response.json() data = await response.json()
@ -124,9 +141,9 @@ class Bible(commands.Cog):
"_get_verses executed with a response code of: %s", response.status "_get_verses executed with a response code of: %s", response.status
) )
if response.status == 401: if response.status == 401:
raise Unauthorized("Missing/Invalid API key.") raise bible.errors.Unauthorized("Missing/Invalid API key.")
if response.status == 403: if response.status == 403:
raise BibleAccessError( raise bible.errors.BibleAccessError(
"The provided API key cannot retrieve sections from the configured Bible." "The provided API key cannot retrieve sections from the configured Bible."
) )
data = await response.json() data = await response.json()