Skip to content

Commit ced23aa

Browse files
committed
refactor(error_handler): enhanced code
1 parent 331f305 commit ced23aa

File tree

6 files changed

+24
-19
lines changed

6 files changed

+24
-19
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.1
14+
rev: v0.5.3
1515
hooks:
1616
- id: ruff-format
1717
- id: ruff

docs/source/api/decorators.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,4 @@ Details
6161
.. autodecorator:: hydrogram.Client.on_poll()
6262
.. autodecorator:: hydrogram.Client.on_disconnect()
6363
.. autodecorator:: hydrogram.Client.on_raw_update()
64+
.. autodecorator:: hydrogram.Client.on_error()

hydrogram/crypto/aes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ def xor(a: bytes, b: bytes) -> bytes:
5050
len(a),
5151
"big",
5252
)
53+
5354
except ImportError:
5455
import pyaes
5556

hydrogram/handlers/error_handler.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
if TYPE_CHECKING:
2727
import hydrogram
28+
from hydrogram.types import Update
2829

2930

3031
class ErrorHandler(Handler):
@@ -47,26 +48,29 @@ class ErrorHandler(Handler):
4748
client (:obj:`~hydrogram.Client`):
4849
The Client itself, useful when you want to call other API methods inside the error handler.
4950
50-
error (``Exception``):
51-
The error that was raised.
52-
5351
update (:obj:`~hydrogram.Update`):
5452
The update that caused the error.
53+
54+
error (``Exception``):
55+
The error that was raised.
5556
"""
5657

5758
def __init__(
58-
self, callback: Callable, errors: type[Exception] | Iterable[type[Exception]] | None = None
59+
self,
60+
callback: Callable,
61+
errors: type[Exception] | Iterable[type[Exception]] | None = None,
5962
):
60-
if errors is None:
61-
errors = [Exception]
62-
elif not isinstance(errors, Iterable):
63-
errors = [errors]
64-
65-
self.errors = errors
63+
self.errors = (
64+
tuple(errors)
65+
if isinstance(errors, Iterable)
66+
else (errors,)
67+
if errors
68+
else (Exception,)
69+
)
6670
super().__init__(callback)
6771

68-
async def check(self, client: hydrogram.Client, error: Exception):
69-
return any(isinstance(error, e) for e in self.errors)
72+
async def check(self, client: hydrogram.Client, update: Update, error: Exception) -> bool:
73+
return isinstance(error, self.errors)
7074

71-
def check_remove(self, error: Exception):
72-
return self.errors == error or any(isinstance(error, e) for e in self.errors)
75+
def check_remove(self, error: Exception) -> bool:
76+
return isinstance(error, self.errors)

pyproject.toml

Lines changed: 3 additions & 3 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.1",
54+
"ruff>=0.5.3",
5555
"pytest>=7.4.3",
5656
"pytest-asyncio>=0.23.2",
5757
"pytest-cov>=4.1.0",
@@ -61,8 +61,8 @@ dev-dependencies = [
6161

6262
[project.optional-dependencies]
6363
docs = [
64-
"Sphinx>=7.3.7",
65-
"furo>=2024.5.6",
64+
"Sphinx>=7.4.6",
65+
"furo>=2024.7.18",
6666
"sphinx-autobuild>=2024.4.16",
6767
"sphinx-copybutton>=0.5.2",
6868
"pygments>=2.18.0",

tests/filters/test_command.py

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

20-
2120
from hydrogram import filters
2221
from tests.filters import Client, Message
2322

0 commit comments

Comments
 (0)