|
| 1 | +from typing import cast |
| 2 | + |
1 | 3 | from aiogram import Router |
| 4 | +from aiogram.exceptions import TelegramBadRequest |
| 5 | +from aiogram.filters import ExceptionTypeFilter |
2 | 6 | from aiogram.types import ErrorEvent |
| 7 | +from aiogram_dialog import DialogManager, StartMode |
| 8 | +from aiogram_dialog.api.exceptions import OutdatedIntent, UnknownIntent |
3 | 9 | from dishka.integrations.aiogram import FromDishka, inject |
4 | 10 | from structlog.types import FilteringBoundLogger |
5 | 11 |
|
6 | 12 | from ttt.infrastructure.structlog.logger import unexpected_error_log |
| 13 | +from ttt.presentation.aiogram_dialog.main_dialog.common import MainDialogState |
7 | 14 |
|
8 | 15 |
|
9 | 16 | error_handling_router = Router(name=__name__) |
10 | 17 |
|
11 | 18 |
|
| 19 | +@error_handling_router.error( |
| 20 | + ExceptionTypeFilter(UnknownIntent, OutdatedIntent), |
| 21 | +) |
| 22 | +async def _(_: ErrorEvent, dialog_manager: DialogManager) -> None: |
| 23 | + await dialog_manager.start( |
| 24 | + MainDialogState.main, |
| 25 | + {"hint": "Сессия устарела"}, |
| 26 | + StartMode.RESET_STACK, |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +@error_handling_router.error(ExceptionTypeFilter(TelegramBadRequest)) |
| 31 | +@inject |
| 32 | +async def _( |
| 33 | + event: ErrorEvent, |
| 34 | + logger: FromDishka[FilteringBoundLogger], |
| 35 | +) -> None: |
| 36 | + error = cast(TelegramBadRequest, event.exception) |
| 37 | + |
| 38 | + if error.message not in { |
| 39 | + "Bad Request: chat not found", |
| 40 | + "Forbidden: bot was blocked by the user", |
| 41 | + }: |
| 42 | + await unexpected_error_log(logger, error) |
| 43 | + |
| 44 | + |
12 | 45 | @error_handling_router.error() |
13 | 46 | @inject |
14 | 47 | async def _( |
|
0 commit comments