From 552f00ff32f900ed54ef7634882dc96f7045dcf3 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Sat, 27 Mar 2021 15:31:08 +0100 Subject: [PATCH] :stop_sign: Better exception handling --- Gwendolyn.py | 11 +++++++++-- cogs/MiscCog.py | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Gwendolyn.py b/Gwendolyn.py index be4abab..a804f9c 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -1,4 +1,4 @@ -import discord, os, finnhub, platform, asyncio +import discord, os, finnhub, platform, asyncio, traceback from discord.ext import commands from pymongo import MongoClient @@ -86,7 +86,14 @@ async def on_command_error(ctx, error): logThis(f"{error}",str(ctx.message.channel.id)) await ctx.send("Missing command parameters (error code 002). Try using `!help [command]` to find out how to use the command.") else: - logThis(f"Something went wrong, {error}, ({type(error)})",str(ctx.message.channel.id)) + exception = traceback.format_exception(type(error), error, error.__traceback__) + stopAt = "\nThe above exception was the direct cause of the following exception:\n\n" + if stopAt in exception: + index = exception.index(stopAt) + exception = exception[:index] + + exceptionString = "".join(exception) + logThis(f"exception in command !{ctx.command}, {exceptionString}",str(ctx.message.channel.id)) await ctx.send("Something went wrong (error code 000)") #Loads cogs diff --git a/cogs/MiscCog.py b/cogs/MiscCog.py index e9cb0f3..91f560a 100644 --- a/cogs/MiscCog.py +++ b/cogs/MiscCog.py @@ -45,6 +45,7 @@ class MiscCog(commands.Cog): self.client.funcs.stopServer() + logThis("Logging out.") await self.client.logout() else: logThis(f"{ctx.message.author.display_name} tried to stop me! (error code 201)",str(ctx.message.channel.id))