Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated local error handling #303

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions bot/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
os.environ.update(JISHAKU_NO_UNDERSCORE="True", JISHAKU_NO_DM_TRACEBACK="True", JISHAKU_HIDE="True")
ALLOWED_MENTIONS = discord.AllowedMentions(everyone=False, roles=False, users=False)

RUNNING_IN_KUBERNETES = os.environ.get("KUBERNETES_SERVICE_HOST") is not None


class DiscordBot(commands.Bot):
def __init__(self, prefixes: tuple[str, ...], extensions: tuple[str, ...], intents: discord.Intents):
Expand Down Expand Up @@ -146,7 +148,7 @@ async def on_app_command_completion(
VALUES ($1, $2, $3, $4, $5, $6, $7)
"""

res = await Model.pool.execute(
await Model.pool.execute(
query,
interaction.user.id,
interaction.guild_id,
Expand All @@ -156,11 +158,13 @@ async def on_app_command_completion(
command.qualified_name,
interaction.data["options"],
)
log.info(f"Command usage inserted {res}")
except Exception as e:
await self.on_error("on_app_command_completion", e)

async def send_error(self, content: str, header: str, invoked_details_document: Document = None) -> None:
if not RUNNING_IN_KUBERNETES:
return

def wrap(code: str) -> str:
code = code.replace("`", "\u200b`")
return f"```py\n{code}\n```"
Expand All @@ -180,10 +184,12 @@ def wrap(code: str) -> str:
await self.error_webhook.send(embed=embed)

async def on_error(self, event_method: str, *args: Any, **kwargs: Any) -> None:
content = "".join(traceback.format_exception(*sys.exc_info()))
error = sys.exc_info()
content = "".join(traceback.format_exception(*error))
header = f"Ignored exception in event method **{event_method}**"

await self.send_error(content, header)
log.error(f"Ignored exception in event method {event_method}", exc_info=error)

async def on_app_command_error(self, interaction: "InteractionType", error: app_commands.AppCommandError):
"""Handle errors in app commands."""
Expand Down
6 changes: 6 additions & 0 deletions bot/extensions/youtube/tasks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import re
import traceback
import xml.etree.ElementTree as ET
Expand All @@ -15,6 +16,9 @@
RSS_FEED_BASE_URL = "https://www.youtube.com/feeds/videos.xml"


log = logging.getLogger(__name__)


class Video(BaseModel):
link: str
title: str
Expand Down Expand Up @@ -100,6 +104,8 @@ async def find_new_videos(self):
header = "Ignored exception when parsing rss feed."
document = await paste.create(content=data)

log.error(header, exc_info=e)

return await self.bot.send_error(content, header, invoked_details_document=document)

ns = "{http://www.w3.org/2005/Atom}"
Expand Down
Loading