Skip to content

Commit 2c8de1d

Browse files
committed
refactor(error_handler): better code consistency
1 parent 013268c commit 2c8de1d

File tree

5 files changed

+37
-30
lines changed

5 files changed

+37
-30
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ repos:
1111
- id: check-yaml
1212

1313
- repo: https://github.com/astral-sh/ruff-pre-commit
14-
rev: v0.5.3
14+
rev: v0.5.4
1515
hooks:
1616
- id: ruff-format
1717
- id: ruff

hydrogram/dispatcher.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,9 @@ async def _process_packet(self, packet, lock):
232232

233233
async def _handle_update(self, handler, handler_type, parsed_update, update, users, chats):
234234
try:
235-
if isinstance(handler, handler_type) and await handler.check(
236-
self.client, parsed_update
237-
):
238-
await self._execute_callback(handler, parsed_update)
235+
if isinstance(handler, handler_type):
236+
if await handler.check(self.client, parsed_update):
237+
await self._execute_callback(handler, parsed_update)
239238
elif isinstance(handler, RawUpdateHandler):
240239
await self._execute_callback(handler, update, users, chats)
241240
except (hydrogram.StopPropagation, hydrogram.ContinuePropagation) as e:
@@ -249,17 +248,17 @@ async def _handle_exception(self, parsed_update, exception):
249248
for error_handler in self.error_handlers:
250249
try:
251250
if await error_handler.check(self.client, parsed_update, exception):
252-
await error_handler.callback(self.client, parsed_update, exception)
253251
handled_error = True
254252
break
255253
except hydrogram.StopPropagation:
256254
raise
257-
except (hydrogram.ContinuePropagation, Exception) as inner_exception:
258-
if isinstance(inner_exception, Exception):
259-
log.exception(inner_exception)
255+
except hydrogram.ContinuePropagation:
256+
continue
257+
except Exception as inner_exception:
258+
log.exception("Error in error handler: %s", inner_exception)
260259

261260
if not handled_error:
262-
log.exception(exception)
261+
log.exception("Unhandled exception: %s", exception)
263262

264263
async def _execute_callback(self, handler, *args):
265264
if inspect.iscoroutinefunction(handler.callback):

hydrogram/handlers/error_handler.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ErrorHandler(Handler):
4040
Pass a function that will be called when a new Error arrives. It takes *(client, error)*
4141
as positional arguments (look at the section below for a detailed description).
4242
43-
errors (``Exception`` | Iterable of ``Exception``, *optional*):
43+
exceptions (``Exception`` | Iterable of ``Exception``, *optional*):
4444
Pass one or more exception classes to allow only a subset of errors to be passed
4545
in your callback function.
4646
@@ -58,19 +58,22 @@ class ErrorHandler(Handler):
5858
def __init__(
5959
self,
6060
callback: Callable,
61-
errors: type[Exception] | Iterable[type[Exception]] | None = None,
61+
exceptions: type[Exception] | Iterable[type[Exception]] | None = None,
6262
):
63-
self.errors = (
64-
tuple(errors)
65-
if isinstance(errors, Iterable)
66-
else (errors,)
67-
if errors
63+
self.exceptions = (
64+
tuple(exceptions)
65+
if isinstance(exceptions, Iterable)
66+
else (exceptions,)
67+
if exceptions
6868
else (Exception,)
6969
)
7070
super().__init__(callback)
7171

72-
async def check(self, client: hydrogram.Client, update: Update, error: Exception) -> bool:
73-
return isinstance(error, self.errors)
72+
async def check(self, client: hydrogram.Client, update: Update, exception: Exception) -> bool:
73+
if isinstance(exception, self.exceptions):
74+
await self.callback(client, update, exception)
75+
return True
76+
return False
7477

75-
def check_remove(self, error: Exception) -> bool:
76-
return isinstance(error, self.errors)
78+
def check_remove(self, error: type[Exception] | Iterable[type[Exception]]) -> bool:
79+
return isinstance(error, self.exceptions)

hydrogram/methods/utilities/remove_error_handler.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,19 @@
2828

2929
class RemoveErrorHandler:
3030
def remove_error_handler(
31-
self: hydrogram.Client, error: type[Exception] | Iterable[type[Exception]] = Exception
31+
self: hydrogram.Client,
32+
exception: type[Exception] | Iterable[type[Exception]] = Exception,
3233
):
33-
"""Remove a previously-registered error handler. (using exception classes)
34+
"""Remove a previously registered error handler using exception classes.
3435
3536
Parameters:
36-
error (``Exception`` | Iterable of ``Exception``, *optional*):
37-
The error(s) for handlers to be removed.
37+
exception (``Exception`` | Iterable of ``Exception``, *optional*):
38+
The error(s) for handlers to be removed. Defaults to Exception.
3839
"""
39-
for handler in self.dispatcher.error_handlers:
40-
if handler.check_remove(error):
41-
self.dispatcher.error_handlers.remove(handler)
40+
to_remove = [
41+
handler
42+
for handler in self.dispatcher.error_handlers
43+
if handler.check_remove(exception)
44+
]
45+
for handler in to_remove:
46+
self.dispatcher.error_handlers.remove(handler)

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ build-backend = "hatchling.build"
5151
[tool.rye]
5252
managed = true
5353
dev-dependencies = [
54-
"ruff>=0.5.3",
54+
"ruff>=0.5.4",
5555
"pytest>=7.4.3",
5656
"pytest-asyncio>=0.23.2",
5757
"pytest-cov>=4.1.0",
@@ -61,7 +61,7 @@ dev-dependencies = [
6161

6262
[project.optional-dependencies]
6363
docs = [
64-
"Sphinx>=7.4.6",
64+
"sphinx>=7.4.7",
6565
"furo>=2024.7.18",
6666
"sphinx-autobuild>=2024.4.16",
6767
"sphinx-copybutton>=0.5.2",

0 commit comments

Comments
 (0)