forked from blizzthewolf/SeaCogs
fix(bible): fixing error handling
This commit is contained in:
parent
6df92da12f
commit
2841fb12a5
2 changed files with 70 additions and 61 deletions
|
@ -44,7 +44,7 @@ class Bible(commands.Cog):
|
||||||
for book in books:
|
for book in books:
|
||||||
if book_name in (book["abbreviation"], book["name"]):
|
if book_name in (book["abbreviation"], book["name"]):
|
||||||
return book["id"]
|
return book["id"]
|
||||||
raise ValueError(f"Book {book_name} not found.")
|
raise ValueError(error(f"Book {book_name} not found."))
|
||||||
|
|
||||||
async def _get_passage(
|
async def _get_passage(
|
||||||
self, bible_id: str, passage_id: str, include_verse_numbers: bool
|
self, bible_id: str, passage_id: str, include_verse_numbers: bool
|
||||||
|
@ -67,13 +67,11 @@ 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 bible.errors.Unauthorized("Missing/Invalid API key.")
|
raise bible.errors.Unauthorized()
|
||||||
if response.status == 403:
|
if response.status == 403:
|
||||||
raise bible.errors.BibleAccessError(
|
raise bible.errors.BibleAccessError()
|
||||||
"The provided API key cannot retrieve sections from the configured Bible."
|
|
||||||
)
|
|
||||||
if response.status == 404:
|
if response.status == 404:
|
||||||
raise bible.errors.NotFound("The requested passage was not found.")
|
raise bible.errors.NotFound()
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
return data["data"]
|
return data["data"]
|
||||||
|
|
||||||
|
@ -87,11 +85,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 bible.errors.Unauthorized("Missing/Invalid API key.")
|
raise bible.errors.Unauthorized()
|
||||||
if response.status == 403:
|
if response.status == 403:
|
||||||
raise bible.errors.BibleAccessError(
|
raise bible.errors.BibleAccessError()
|
||||||
"The provided API key cannot retrieve sections from the configured Bible."
|
|
||||||
)
|
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
return data["data"]
|
return data["data"]
|
||||||
|
|
||||||
|
@ -105,11 +101,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 bible.errors.Unauthorized("Missing/Invalid API key.")
|
raise bible.errors.Unauthorized()
|
||||||
if response.status == 403:
|
if response.status == 403:
|
||||||
raise bible.errors.BibleAccessError(
|
raise bible.errors.BibleAccessError()
|
||||||
"The provided API key cannot retrieve sections from the configured Bible."
|
|
||||||
)
|
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
return data["data"]
|
return data["data"]
|
||||||
|
|
||||||
|
@ -123,11 +117,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 bible.errors.Unauthorized("Missing/Invalid API key.")
|
raise bible.errors.Unauthorized()
|
||||||
if response.status == 403:
|
if response.status == 403:
|
||||||
raise bible.errors.BibleAccessError(
|
raise bible.errors.BibleAccessError()
|
||||||
"The provided API key cannot retrieve sections from the configured Bible."
|
|
||||||
)
|
|
||||||
data = await response.json()
|
data = await response.json()
|
||||||
return data["data"]
|
return data["data"]
|
||||||
|
|
||||||
|
@ -150,6 +142,7 @@ class Bible(commands.Cog):
|
||||||
await ctx.send(str(e))
|
await ctx.send(str(e))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
if len(passage.split("-")) == 2:
|
if len(passage.split("-")) == 2:
|
||||||
from_verse, to_verse = passage.replace(":", ".").split("-")
|
from_verse, to_verse = passage.replace(":", ".").split("-")
|
||||||
if "." not in to_verse:
|
if "." not in to_verse:
|
||||||
|
@ -161,6 +154,13 @@ class Bible(commands.Cog):
|
||||||
passage = await self._get_passage(
|
passage = await self._get_passage(
|
||||||
bible_id, f"{book_id}.{passage.replace(':', '.')}", False
|
bible_id, f"{book_id}.{passage.replace(':', '.')}", False
|
||||||
)
|
)
|
||||||
|
except (
|
||||||
|
bible.errors.BibleAccessError,
|
||||||
|
bible.errors.NotFound,
|
||||||
|
bible.errors.Unauthorized,
|
||||||
|
) as e:
|
||||||
|
await ctx.send(e.message)
|
||||||
|
return
|
||||||
|
|
||||||
if len(passage["content"]) > 4096:
|
if len(passage["content"]) > 4096:
|
||||||
await ctx.send("The passage is too long to send.")
|
await ctx.send("The passage is too long to send.")
|
||||||
|
@ -179,6 +179,7 @@ class Bible(commands.Cog):
|
||||||
"""Get a random Bible verse."""
|
"""Get a random Bible verse."""
|
||||||
bible_id = await self.config.bible()
|
bible_id = await self.config.bible()
|
||||||
|
|
||||||
|
try:
|
||||||
books = await self._get_books(bible_id)
|
books = await self._get_books(bible_id)
|
||||||
book = random.choice(books)
|
book = random.choice(books)
|
||||||
|
|
||||||
|
@ -189,6 +190,13 @@ class Bible(commands.Cog):
|
||||||
verse = random.choice(verses)["id"]
|
verse = random.choice(verses)["id"]
|
||||||
|
|
||||||
passage = await self._get_passage(bible_id, verse, False)
|
passage = await self._get_passage(bible_id, verse, False)
|
||||||
|
except (
|
||||||
|
bible.errors.BibleAccessError,
|
||||||
|
bible.errors.NotFound,
|
||||||
|
bible.errors.Unauthorized,
|
||||||
|
) as e:
|
||||||
|
await ctx.send(e.message)
|
||||||
|
return
|
||||||
|
|
||||||
embed = Embed(
|
embed = Embed(
|
||||||
title=f"{passage['reference']}",
|
title=f"{passage['reference']}",
|
||||||
|
@ -197,25 +205,3 @@ class Bible(commands.Cog):
|
||||||
)
|
)
|
||||||
embed.set_footer(text=f"{ctx.prefix}bible random - Powered by API.bible")
|
embed.set_footer(text=f"{ctx.prefix}bible random - Powered by API.bible")
|
||||||
await ctx.send(embed=embed)
|
await ctx.send(embed=embed)
|
||||||
|
|
||||||
@bible_passage.error
|
|
||||||
@bible_random.error
|
|
||||||
async def error_handler(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 you are the bot owner, please check the documentation [here](https://seacogs.coastalcommits.com/bible/#setup)."""
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return
|
|
||||||
elif 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."
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return
|
|
||||||
elif isinstance(err, bible.errors.NotFound):
|
|
||||||
await ctx.send(error("The requested passage was not found."))
|
|
||||||
return
|
|
||||||
|
|
|
@ -1,10 +1,33 @@
|
||||||
|
from redbot.core.utils.chat_formatting import error
|
||||||
|
|
||||||
|
|
||||||
class BibleAccessError(Exception):
|
class BibleAccessError(Exception):
|
||||||
pass
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str = error(
|
||||||
|
"The provided API key cannot retrieve sections from the configured Bible. Please report this to the bot owner."
|
||||||
|
),
|
||||||
|
):
|
||||||
|
super().__init__(message)
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
class Unauthorized(Exception):
|
class Unauthorized(Exception):
|
||||||
pass
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str = error(
|
||||||
|
"""The API key for API.bible is missing or invalid. Please report this to the bot owner.
|
||||||
|
If you are the bot owner, please check the documentation [here](https://seacogs.coastalcommits.com/bible/#setup)."""
|
||||||
|
),
|
||||||
|
):
|
||||||
|
super().__init__(message)
|
||||||
|
self.message = message
|
||||||
|
|
||||||
|
|
||||||
class NotFound(Exception):
|
class NotFound(Exception):
|
||||||
pass
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str = error("The requested passage was not found."),
|
||||||
|
):
|
||||||
|
super().__init__(message)
|
||||||
|
self.message = message
|
||||||
|
|
Loading…
Reference in a new issue