Skip to content

Commit

Permalink
feat!: Client.on_error() improvement with new parameter (#1945)
Browse files Browse the repository at this point in the history
Signed-off-by: Guard Boi <[email protected]>
Signed-off-by: Lala Sabathil <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Om <[email protected]>
Co-authored-by: Lala Sabathil <[email protected]>
  • Loading branch information
4 people authored May 31, 2023
1 parent e3ee572 commit 7f192fe
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ These changes are available on the `master` branch, but have not yet been releas
new theme colors in the app.
([#1931](https://github.com/Pycord-Development/pycord/pull/1931))

### Changed

- `Client.on_error()` now has an `exception` parameter for easier error handling.
([#1945](https://github.com/Pycord-Development/pycord/pull/1945))

### Fixed

- Fixed the type-hinting of `SlashCommandGroup.walk_commands()` to reflect actual
Expand Down
8 changes: 5 additions & 3 deletions discord/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ async def _run_event(
await coro(*args, **kwargs)
except asyncio.CancelledError:
pass
except Exception:
except Exception as exc:
try:
await self.on_error(event_name, *args, **kwargs)
await self.on_error(event_name, exc, *args, **kwargs)
except asyncio.CancelledError:
pass

Expand Down Expand Up @@ -481,7 +481,9 @@ def dispatch(self, event: str, *args: Any, **kwargs: Any) -> None:
for coro in once_listeners:
self._event_handlers[method].remove(coro)

async def on_error(self, event_method: str, *args: Any, **kwargs: Any) -> None:
async def on_error(
self, event_method: str, exception: Exception, *args: Any, **kwargs: Any
) -> None:
"""|coro|
The default error handler provided by the client.
Expand Down
8 changes: 4 additions & 4 deletions docs/api/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,14 @@ Channels

Connection
----------
.. function:: on_error(event, *args, **kwargs)
.. function:: on_error(event, exception, *args, **kwargs)

Usually when an event raises an uncaught exception, a traceback is
printed to stderr and the exception is ignored. If you want to
change this behaviour and handle the exception for whatever reason
yourself, this event can be overridden. Which, when done, will
suppress the default action of printing the traceback.

The information of the exception raised and the exception itself can
be retrieved with a standard call to :func:`sys.exc_info`.

If you want exception to propagate out of the :class:`Client` class
you can define an ``on_error`` handler consisting of a single empty
:ref:`raise statement <py:raise>`. Exceptions raised by ``on_error`` will not be
Expand All @@ -279,6 +276,9 @@ Connection
:param event: The name of the event that raised the exception.
:type event: :class:`str`

:param exception: The exception raised by the event.
:type event: :class:`Exception`

:param args: The positional arguments for the event that raised the
exception.
:param kwargs: The keyword arguments for the event that raised the
Expand Down

0 comments on commit 7f192fe

Please sign in to comment.