mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-08 17:13:56 +00:00
Add filters and class based handler for errors
This commit is contained in:
parent
9e673998f0
commit
0fbd2819f9
9 changed files with 167 additions and 1 deletions
29
docs/dispatcher/class_based_handlers/error.md
Normal file
29
docs/dispatcher/class_based_handlers/error.md
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
# ErrorHandler
|
||||
|
||||
There is base class for error handlers.
|
||||
|
||||
## Simple usage:
|
||||
```pyhton3
|
||||
from aiogram.handlers import ErrorHandler
|
||||
|
||||
...
|
||||
|
||||
@router.errors_handler()
|
||||
class MyHandler(ErrorHandler):
|
||||
async def handle(self) -> Any:
|
||||
log.exception(
|
||||
"Cause unexpected exception %s: %s",
|
||||
self.event.__class__.__name__,
|
||||
self.event
|
||||
)
|
||||
```
|
||||
|
||||
## Extension
|
||||
|
||||
This base handler is subclass of [BaseHandler](basics.md#basehandler)
|
||||
|
||||
## Related pages
|
||||
|
||||
- [BaseHandler](basics.md#basehandler)
|
||||
- [Router.errors_handler](../router.md#errors)
|
||||
- [Filters](../filters/exception.md)
|
||||
27
docs/dispatcher/filters/exception.md
Normal file
27
docs/dispatcher/filters/exception.md
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
# Exceptions
|
||||
This filters can be helpful for handling errors from the text messages.
|
||||
|
||||
## ExceptionTypeFilter
|
||||
|
||||
Allow to match exception by type
|
||||
|
||||
### Specification
|
||||
| Argument | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| `exception` | `#!python3 Union[Type[Exception], Tuple[Type[Exception]]]` | Exception type(s) |
|
||||
|
||||
|
||||
## ExceptionMessageFilter
|
||||
|
||||
Allow to match exception by message
|
||||
|
||||
### Specification
|
||||
| Argument | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| `match` | `#!python3 Union[str, Pattern[str]]` | Regexp pattern |
|
||||
|
||||
## Allowed handlers
|
||||
|
||||
Allowed update types for this filters:
|
||||
|
||||
- `error`
|
||||
|
|
@ -116,6 +116,13 @@ async def poll_answer_handler(poll_answer: types.PollAnswer) -> Any: pass
|
|||
```
|
||||
Is useful for handling [polls answers](../api/types/poll_answer.md)
|
||||
|
||||
### Errors
|
||||
```python3
|
||||
@router.errors_handler()
|
||||
async def error_handler(exception: Exception) -> Any: pass
|
||||
```
|
||||
Is useful for handling errors from other handlers
|
||||
|
||||
|
||||
## Nested routers
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue