diff options
author | 2019-09-14 15:55:06 -0700 | |
---|---|---|
committer | 2019-09-14 16:08:38 -0700 | |
commit | 64e8e7423e0d1c765b30552d5d5a390df767c9c1 (patch) | |
tree | 5bdf59f12cff5e784fe21790cc9de685d4165854 | |
parent | Display no-DM error message originating from security cog's global check (diff) |
Improve logging of command errors
-rw-r--r-- | bot/cogs/error_handler.py | 41 |
1 files changed, 28 insertions, 13 deletions
diff --git a/bot/cogs/error_handler.py b/bot/cogs/error_handler.py index d65419ae8..59b6c0573 100644 --- a/bot/cogs/error_handler.py +++ b/bot/cogs/error_handler.py @@ -35,6 +35,7 @@ class ErrorHandler: if command is not None: parent = command.parent + # Retrieve the help command for the invoked command. if parent and command: help_command = (self.bot.get_command("help"), parent.name, command.name) elif command: @@ -46,6 +47,7 @@ class ErrorHandler: log.debug(f"Command {command} has a local error handler; ignoring.") return + # Try to look for a tag with the command's name if the command isn't found. if isinstance(e, CommandNotFound) and not hasattr(ctx, "invoked_from_error_handler"): if not ctx.channel.id == Channels.verification: tags_get_command = self.bot.get_command("tags get") @@ -60,6 +62,10 @@ class ErrorHandler: elif isinstance(e, UserInputError): await ctx.send("Something about your input seems off. Check the arguments:") await ctx.invoke(*help_command) + log.debug( + f"Command {command} invoked by {ctx.message.author} with error " + f"{e.__class__.__name__}: {e}" + ) elif isinstance(e, NoPrivateMessage): await ctx.send("Sorry, this command can't be used in a private message!") elif isinstance(e, BotMissingPermissions): @@ -79,26 +85,35 @@ class ErrorHandler: ) elif isinstance(e, CommandInvokeError): if isinstance(e.original, ResponseCodeError): - if e.original.response.status == 404: + status = e.original.response.status + + if status == 404: await ctx.send("There does not seem to be anything matching your query.") - elif e.original.response.status == 400: + elif status == 400: content = await e.original.response.json() - log.debug("API gave bad request on command. Response: %r.", content) + log.debug(f"API responded with 400 for command {command}: %r.", content) await ctx.send("According to the API, your request is malformed.") - elif 500 <= e.original.response.status < 600: + elif 500 <= status < 600: await ctx.send("Sorry, there seems to be an internal issue with the API.") + log.warning(f"API responded with {status} for command {command}") else: - await ctx.send( - "Got an unexpected status code from the " - f"API (`{e.original.response.code}`)." - ) + await ctx.send(f"Got an unexpected status code from the API (`{status}`).") + log.warning(f"Unexpected API response for command {command}: {status}") else: - await ctx.send( - f"Sorry, an unexpected error occurred. Please let us know!\n\n```{e}```" - ) - raise e.original + await self.handle_unexpected_error(ctx, e.original) else: - raise e + await self.handle_unexpected_error(ctx, e) + + @staticmethod + async def handle_unexpected_error(ctx: Context, e: CommandError): + await ctx.send( + f"Sorry, an unexpected error occurred. Please let us know!\n\n" + f"```{e.__class__.__name__}: {e}```" + ) + log.error( + f"Error executing command invoked by {ctx.message.author}: {ctx.message.content}" + ) + raise e def setup(bot: Bot): |