← 返回首页
Error handlers by lrdcxdes · Pull Request #1240 · pyrogram/pyrogram · GitHub
Skip to content

Navigation Menu

Toggle navigation
Sign in
Appearance settings
Search or jump to...

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Include my email address so I can be contacted

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Resetting focus
This repository was archived by the owner on Dec 23, 2024. It is now read-only.

Error handlers#1240

Open
lrdcxdes wants to merge 5 commits into
pyrogram:masterfrom
lrdcxdes:master
Open

Error handlers#1240
lrdcxdes wants to merge 5 commits into
pyrogram:masterfrom
lrdcxdes:master

Conversation

lrdcxdes commented Mar 21, 2023
edited
Loading

Copy link
Copy Markdown
Contributor
from pyrogram import Client, filters app = Client(...)

Use with decorator

@app.on_error() # Global error handler, all other errors will be sent to it async def err_handler(_, e: Exception, update): print(e, 'handled') print(update) @app.on_error(ValueError) # Also you can use tuple with many exception classes, example: (ValueError, TypeError) async def custom_err_handler(_, e: ValueError, update): print(e, 'custom handled') print(update)

Use without a decorator

value_error_handler = ErrorHandler(custom_err_handler, ValueError) # Also you can use massive with many exception classes, example: (ValueError, TypeError) global_error_handler = ErrorHandler(err_handler) # Global error handler, all other errors will be sent to it app.add_handler(value_error_handler) app.add_handler(global_error_handler)

Startup example

@app.on_message(filters.command('testerror')) async def msg_handler(_, m): raise Exception('TestError 123') @app.on_message(filters.command('valueerror')) async def custom_msg_handler(_, m): raise ValueError('TestError 123') app.run()

Remove error handler

app.remove_handler(value_error_handler) # Or you can use app.remove_error_handler(error=ValueError) # to remove all error handlers with ValueError in .errors # For remove global error handler, you can use: app.remove_error_handler()

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Footer

© 2026 GitHub, Inc.