Skip to content

Commit 2b4d6bc

Browse files
chore(error-handler): some doc and typing hints improvements
1 parent 4ef5aa2 commit 2b4d6bc

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

hydrogram/handlers/error_handler.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Hydrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19+
from __future__ import annotations
20+
21+
from collections.abc import Iterable
1922
from typing import TYPE_CHECKING, Callable
2023

2124
from .handler import Handler
@@ -29,38 +32,40 @@ class ErrorHandler(Handler):
2932
It is intended to be used with :meth:`~hydrogram.Client.add_handler`
3033
3134
For a nicer way to register this handler, have a look at the
32-
:meth:`~hydrogram.Client.on_message` decorator.
35+
:meth:`~hydrogram.Client.on_error` decorator.
3336
3437
Parameters:
3538
callback (``Callable``):
3639
Pass a function that will be called when a new Error arrives. It takes *(client, error)*
3740
as positional arguments (look at the section below for a detailed description).
3841
39-
errors (:obj:`Exception` | List of :obj:`Exception`):
42+
errors (``Exception`` | Iterable of ``Exception``, *optional*):
4043
Pass one or more exception classes to allow only a subset of errors to be passed
4144
in your callback function.
4245
4346
Other parameters:
4447
client (:obj:`~hydrogram.Client`):
45-
The Client itself, useful when you want to call other API methods inside the message handler.
48+
The Client itself, useful when you want to call other API methods inside the error handler.
4649
47-
error (:obj:`~Exception`):
50+
error (``Exception``):
4851
The error that was raised.
4952
5053
update (:obj:`~hydrogram.Update`):
5154
The update that caused the error.
5255
"""
5356

54-
def __init__(self, callback: Callable, errors=None):
57+
def __init__(
58+
self, callback: Callable, errors: type[Exception] | Iterable[type[Exception]] | None = None
59+
):
5560
if errors is None:
5661
errors = [Exception]
57-
elif not isinstance(errors, list):
62+
elif not isinstance(errors, Iterable):
5863
errors = [errors]
5964

6065
self.errors = errors
6166
super().__init__(callback)
6267

63-
async def check(self, client: "hydrogram.Client", error: Exception):
68+
async def check(self, client: hydrogram.Client, error: Exception):
6469
return any(isinstance(error, e) for e in self.errors)
6570

6671
def check_remove(self, error: Exception):

hydrogram/methods/utilities/remove_error_handler.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,19 @@
2121
from typing import TYPE_CHECKING
2222

2323
if TYPE_CHECKING:
24+
from collections.abc import Iterable
25+
2426
import hydrogram
2527

2628

2729
class RemoveErrorHandler:
2830
def remove_error_handler(
29-
self: hydrogram.Client, error: Exception | tuple[Exception] = Exception
31+
self: hydrogram.Client, error: type[Exception] | Iterable[type[Exception]] = Exception
3032
):
3133
"""Remove a previously-registered error handler. (using exception classes)
3234
3335
Parameters:
34-
error (``Exception``):
36+
error (``Exception`` | Iterable of ``Exception``, *optional*):
3537
The error(s) for handlers to be removed.
3638
"""
3739
for handler in self.dispatcher.error_handlers:

0 commit comments

Comments
 (0)