aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-04-29 16:51:03 -0700
committerGravatar MarkKoz <[email protected]>2020-04-29 16:56:57 -0700
commit2c48aa978ece0b26c158faa6080fc16649943eed (patch)
tree000ea4e81dfbf61b7de5062f9933113257b10cec
parentMerge pull request #913 from python-discord/add-bottom-sorting-to-help-channels (diff)
Log unhandled errors from event listeners
By default, discord.py prints them to stderr. To better help detect such errors in production, they should instead be logged with an appropriate log level. Some sentry metadata has also been included. `on_error` doesn't work as a listener in a cog so it's been put in the Bot subclass. Fixes #911
-rw-r--r--bot/bot.py12
1 files changed, 12 insertions, 0 deletions
diff --git a/bot/bot.py b/bot/bot.py
index 6dd5ba896..49fac27e8 100644
--- a/bot/bot.py
+++ b/bot/bot.py
@@ -7,6 +7,7 @@ from typing import Optional
import aiohttp
import discord
from discord.ext import commands
+from sentry_sdk import push_scope
from bot import DEBUG_MODE, api, constants
from bot.async_stats import AsyncStatsClient
@@ -155,3 +156,14 @@ class Bot(commands.Bot):
gateway event before giving up and thus not populating the cache for unavailable guilds.
"""
await self._guild_available.wait()
+
+ async def on_error(self, event: str, *args, **kwargs) -> None:
+ """Log errors raised in event listeners rather than printing them to stderr."""
+ self.stats.incr(f"errors.event.{event}")
+
+ with push_scope() as scope:
+ scope.set_tag("event", event)
+ scope.set_extra("args", args)
+ scope.set_extra("kwargs", kwargs)
+
+ log.exception(f"Unhandled exception in {event}.")