pyrogram icon indicating copy to clipboard operation
pyrogram copied to clipboard

Errors handler

Open lrdcxdes opened this issue 2 years ago • 5 comments

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

lrdcxdes avatar Apr 16 '22 19:04 lrdcxdes

this is a better way to implement it than https://github.com/pyrogram/pyrogram/pull/597

AndrielFR avatar Apr 17 '22 14:04 AndrielFR

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

Danipulok avatar Apr 25 '22 15:04 Danipulok

@delivrance is there any way you could merge it?

Danipulok avatar Jun 22 '22 21:06 Danipulok

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

clot27 avatar Jun 29 '22 13:06 clot27

@delivrance any update?

Danipulok avatar Jun 29 '22 14:06 Danipulok