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

Better error handling ( Nextcord provided handlers) #1058

Open
Gangsta-Muffin opened this issue May 24, 2023 · 0 comments
Open

Better error handling ( Nextcord provided handlers) #1058

Gangsta-Muffin opened this issue May 24, 2023 · 0 comments

Comments

@Gangsta-Muffin
Copy link

Summary

This feature should allow developers to be able to easily create custom error handling via external files, in a similar way that they can create custom help commands

What is the feature request for?

nextcord.ext.commands

The Problem

The issue this feature is designed to tackle, is that there is yet to be an easy and dynamic way of tackling error handling in external files. In my help thread (https://discord.com/channels/881118111967883295/1110347020666941500) I attempted to tackle the creation of error handling using cogs, however, soon ran into the issue that listeners do not override the default callback from the Nextcord library (as pointed out in Ethan's first message).

The main issue, is that it is not possible to create error handling in external files dynamically. More specifically, the only solution there is to creating events in external files for error handling, is by manually overriding each and every error event from nextcord.ext.commands.Bot. This issue does, however, not only apply to the error events, but also the other events such as the on_ready event (assuming you would want to override that method and have it run in an alternate file.

The Ideal Solution

Ideally, it would be cool to have 1 or 2 Nextcord classes that could be subclassed, like we can with custom Helpcommands using nextcord.ext.commands.HelpCommand classes. These developer created subclasses would then be passed to nextcord.ext.commands.Bot as an argument.

At bare minimum, I would hope to have 1 class, which solely tackles the following error events:

  • on_error
  • on_command_error
  • on_application_command_error

Here is how I imagine the class would get used:

# error_handler.py
# ------------------

from nextcord.ext import commands

class MyErrorHandler(commands.ErrorHandler):
    def __init__(self):
        super().__init__()

    async def on_error(self, event_error, *args, **kwargs):
        # Developer created error handling here

    async def on_command_error(self, ctx, exception):
        # Developer created error handling here

    async def on_application_command_error(self, interaction, exception):
        # Developer created error handling here



# main.py
# ---------

from nextcord.ext import commands
from error_handler import MyErrorHandler

bot = commands.Bot(prefix="!", error_handler=MyErrorHandler(), ....)

The error handling methods in the nextcord.ext.commands.ErrorHanlder subclass are to be named after the event they are supposed to be replacing. My thought would be, that if a developer doesn't create their own error handling for one of more of these events, but passes their subclass to their bot instance, then the parent class (nextcord.ext.commands.ErrorHandler) would have it's own "fancy" error handling (just something simple like sending an embed message to discord with the error/traceback or something) which developers could use. This would probably want to be toggleable though.

Regarding my original statement:

it would be cool to have 1 or 2 Nextcord classes

I was thinking, that this could potentially even be applied to further nextcord events. For example:

  • 1 class purely for error handling, then maybe
  • A 2nd class for all nextcord events (if an event is not created/overriden in the subclass, it would just call the default nextcord method)
  • Perhaps further groups such as guild events (covering events such as creating/editing/deleting channels, thread events, etc etc)? - This does, however, branch out of the purpose of this issue though. This issue is supposed to solely focus on easy error handling creation (and perhaps even offer a template error handler if people are lazy)

Example of using this Nextcord error handler, if they didn't want to make their own:

# main.py
-----------
from nextcord.ext import commands

bot = commands.Bot(error_handler=commands.ErrorHandler, ...)

# or

bot = commands.Bot(...)
bot.error_handler = commands.ErrorHandler()

The Current Solution

The current solution is to override the nextcord.ext.commands.Bot event attributes. I personally do it in the init duner method of my ErrorHandler class as I feel like that is the best version, however there are other ways (example 2):

# error_handler.py
# ------------------

class ErrorHandler:
    def __init__(self, bot):
        bot.on_error = self.on_error
        bot.on_command_error = self.on_command_error
        bot.on_application_command_error = self.on_application_command_error

    async def on_error(self, event_error, *args, **kwargs):
        # Developer created error handling here

    async def on_command_error(self, ctx, exception):
        # Developer created error handling here

    async def on_application_command_error(self, interaction, exception):
        # Developer created error handling here




# main.py
# ---------

from nextcord.ext import commands
from error_handler import ErrorHandler


bot = commands.Bot(...)

ErrorHandler(bot)

Example 2 (a worse solution imo:

# error_handler.py
# ------------------

async def on_error(self, event_error, *args, **kwargs):
    # Developer created error handling here

async def on_command_error(self, ctx, exception):
    # Developer created error handling here

async def on_application_command_error(self, interaction, exception):
    # Developer created error handling here




# main.py
# ---------

from nextcord.ext import commands

from error_handler import *  
# or
from error_hanlder import (
    on_error, 
    on_command_error,
    on_application_command_error
)


bot = commands.Bot(...)

bot.on_error = on_error
bot.on_command_error = on_command_error
bot.on_application_command_error = on_application_command_error

Additional Context

I personally feel that adding this suggestion would make working with Nextcord easier, in particular when it comes to creating error handlers and maintaining decent code architecture in the process. Additionally, I think it would be nice if Nextcord offered a simple error handler people could use, if they didn't want to make their own :)

@Gangsta-Muffin Gangsta-Muffin changed the title Feat: Easier Nextcord error handling Feat: Better error handling ( Nextcord provided handlers) May 25, 2023
@ooliver1 ooliver1 changed the title Feat: Better error handling ( Nextcord provided handlers) Better error handling ( Nextcord provided handlers) Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant