Skip to content

fix: improved default error handler#1818

Open
mifuyutsuki wants to merge 8 commits into
interactions-py:unstablefrom
mifuyutsuki:fix/default-error-handler
Open

fix: improved default error handler#1818
mifuyutsuki wants to merge 8 commits into
interactions-py:unstablefrom
mifuyutsuki:fix/default-error-handler

Conversation

@mifuyutsuki

Copy link
Copy Markdown
Contributor

Pull Request Type

  • Feature addition
  • Bugfix
  • Documentation update
  • Code refactor
  • Tests improvement
  • CI/CD pipeline enhancement
  • Other: [Replace with a description]

Description

The default error handler is now improved with several new fixes, as listed in the Changes section. Notably:

  • ComponentError and ModalError can now send default error messages, consistent with CommandError.
  • BadArgument is now handled with a message to the user.
  • Error traceback now uses Components v2.

Changes

  • Added Client.default_error_send(), which handles sending of default error messages, called by ComponentError and ModalError in addition to CommandError.
  • BadArgument now has its own default error message format, skipping the traceback.
  • Migrated default error message post to Components v2.
    • Allows the error message to be sent if the message was deferred with edit_origin=True regardless of using Components v2 or not. Components v2 messages cannot be edited into Components v1.
  • Instances of CommandException e.g. CommandCheckFailure now log as info instead of error, as they are known to be user errors than bot errors.
    • These errors also no longer post tracebacks. Other errors behave as usual.
  • Added the str() of CommandCheckFailure, showing the name of the failed check function instead of the current one, which is its repr().
  • Updated guide pages for Slash Commands (Checks), Converters (Discord Model Converters), and Events (Frequently Used Events) to note the errors being raised/handled.

Related Issues

Test Scenarios

I've done at least the following:

  • Exception raised in a @slash_command(), dispatches CommandError
  • Exception raised in a @component_callback() with and without defer(edit_origin=True), dispatches ComponentError
  • Exception raised in a @modal_callback() with and without defer(edit_origin=True), dispatches ModalError
  • Check failed, raises CommandCheckFailure and posts "You don't have permission to run this command!"

Python Compatibility

  • I've ensured my code works on Python 3.10.x
  • I've ensured my code works on Python 3.11.x

Checklist

  • I've run the pre-commit code linter over all edited files
  • I've tested my changes on supported Python versions
  • I've added tests for my code, if applicable
  • I've updated / added documentation, where applicable

- With the default error handler, ComponentError and ModalError now post errors, consistent with the handler for CommandError.
- Added an error message post for BadArgument.
- Migrated posted error messages to Components v2 to ensure the error message can be sent on Components v2 messages after defer(edit_origin=True), since a v1 message can be converted into v2 but not the other way around.
Added a message to the CommandCheckFailure class such that its __str__() is a proper error message rather than its repr.
In the default error handler, errors of the type CommandException e.g. CommandOnCooldown are now logged as info with its own log message instead of error. Logging as error would have emitted a traceback and a Sentry log.
Added check for ctx.editing_origin in default_error_send() to determine whether to use send() or edit_origin().

Set content and embeds fields to empty to ensure the function succeeds when the origin message is Components v1.
Added note that failed checks raise CommandCheckFailure and failed Converter conversions raise BadArgument.
Added point that Error by itself only logs errors, not post an error message, but that its subtypes CommandError (as well as ComponentError and ModalError) do.
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

Successfully merging this pull request may close these issues.

2 participants