Visitar URL original
Error handlers by lrdcxdes · Pull Request #1240 · pyrogram/pyrogram · GitHub
Skip to content
This repository was archived by the owner on Dec 23, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove error handler utility added
  • Loading branch information
lrdcxdes committed Mar 21, 2023
commit 38224ff413fcea3cc795a9f210e356aa1fc46b2f
3 changes: 3 additions & 0 deletions pyrogram/handlers/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ def __init__(self, callback: Callable, errors=None):

async def check(self, client: "pyrogram.Client", error: Exception):
return any(isinstance(error, e) for e in self.errors)

async def check_remove(self, error: Exception):
return self.errors == error or any(isinstance(error, e) for e in self.errors)
4 changes: 3 additions & 1 deletion pyrogram/methods/utilities/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
from .start import Start
from .stop import Stop
from .stop_transmission import StopTransmission
from .remove_error_handler import RemoveErrorHandler


class Utilities(
AddHandler,
ExportSessionString,
RemoveHandler,
RemoveErrorHandler,
Restart,
Run,
Start,
Stop,
StopTransmission
StopTransmission,
):
pass
35 changes: 35 additions & 0 deletions pyrogram/methods/utilities/remove_error_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-present Dan <https://github.com/delivrance>
#
# This file is part of Pyrogram.
#
# Pyrogram is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Pyrogram is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.

import pyrogram


class RemoveErrorHandler:
def remove_error_handler(
self: "pyrogram.Client",
error: "Exception | tuple[Exception]" = Exception
):
"""Remove a previously-registered error handler. (using exception classes)

Parameters:
error (``Exception``):
The error(s) for handlers to be removed.
"""
for handler in self.dispatcher.error_handlers:
if await handler.check_remove(error):
self.dispatcher.error_handlers.remove(handler)