Browse Source

[tasks] Add support for passing an argument list of exceptions.

pull/2343/head
mathsman5133 6 years ago
committed by Rapptz
parent
commit
7f65d9a8b1
  1. 35
      discord/ext/tasks/__init__.py

35
discord/ext/tasks/__init__.py

@ -210,8 +210,8 @@ class Loop:
self._task.add_done_callback(restart_when_over) self._task.add_done_callback(restart_when_over)
self._task.cancel() self._task.cancel()
def add_exception_type(self, exc): def add_exception_type(self, *exceptions):
r"""Adds an exception type to be handled during the reconnect logic. r"""Adds exception types to be handled during the reconnect logic.
By default the exception types handled are those handled by By default the exception types handled are those handled by
:meth:`discord.Client.connect`\, which includes a lot of internet disconnection :meth:`discord.Client.connect`\, which includes a lot of internet disconnection
@ -222,21 +222,22 @@ class Loop:
Parameters Parameters
------------ ------------
exc: Type[:class:`BaseException`] \*exceptions: Type[:class:`BaseException`]
The exception class to handle. An argument list of exception classes to handle.
Raises Raises
-------- --------
TypeError TypeError
The exception passed is either not a class or not inherited from :class:`BaseException`. An exception passed is either not a class or not inherited from :class:`BaseException`.
""" """
if not inspect.isclass(exc): for exc in exceptions:
raise TypeError('{0!r} must be a class.'.format(exc)) if not inspect.isclass(exc):
if not issubclass(exc, BaseException): raise TypeError('{0!r} must be a class.'.format(exc))
raise TypeError('{0!r} must inherit from BaseException.'.format(exc)) if not issubclass(exc, BaseException):
raise TypeError('{0!r} must inherit from BaseException.'.format(exc))
self._valid_exception = (*self._valid_exception, exc) self._valid_exception = (*self._valid_exception, *exceptions)
def clear_exception_types(self): def clear_exception_types(self):
"""Removes all exception types that are handled. """Removes all exception types that are handled.
@ -247,22 +248,22 @@ class Loop:
""" """
self._valid_exception = tuple() self._valid_exception = tuple()
def remove_exception_type(self, exc): def remove_exception_type(self, *exceptions):
"""Removes an exception type from being handled during the reconnect logic. r"""Removes exception types from being handled during the reconnect logic.
Parameters Parameters
------------ ------------
exc: Type[:class:`BaseException`] \*exceptions: Type[:class:`BaseException`]
The exception class to handle. An argument list of exception classes to handle.
Returns Returns
--------- ---------
:class:`bool` :class:`bool`
Whether it was successfully removed. Whether all exceptions were successfully removed.
""" """
old_length = len(self._valid_exception) old_length = len(self._valid_exception)
self._valid_exception = tuple(x for x in self._valid_exception if x is not exc) self._valid_exception = tuple(x for x in self._valid_exception if x not in exceptions)
return len(self._valid_exception) != old_length return len(self._valid_exception) == old_length - len(exceptions)
def get_task(self): def get_task(self):
"""Optional[:class:`asyncio.Task`]: Fetches the internal task or ``None`` if there isn't one running.""" """Optional[:class:`asyncio.Task`]: Fetches the internal task or ``None`` if there isn't one running."""

Loading…
Cancel
Save