Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors handler #962

Closed
wants to merge 0 commits into from
Closed

Errors handler #962

wants to merge 0 commits into from

Conversation

lrdcxdes
Copy link
Contributor

@lrdcxdes lrdcxdes commented Apr 16, 2022

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 tuple 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_error_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()

pyrogram/handlers/error_handler.py Outdated Show resolved Hide resolved
pyrogram/dispatcher.py Outdated Show resolved Hide resolved
pyrogram/handlers/error_handler.py Outdated Show resolved Hide resolved
@AndrielFR
Copy link

this is a better way to implement it than #597

@Danipulok
Copy link
Contributor

@delivrance why not merge it?
Seems really good and useful. Can be one of v2.X features

@Danipulok
Copy link
Contributor

@delivrance is there any way you could merge it?

@clot27
Copy link

clot27 commented Jun 29, 2022

Niice work, really liked it. Hope this get merged soon

@Danipulok
Copy link
Contributor

@delivrance any update?

@lrdcxdes lrdcxdes closed this Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants