Skip to content

Commit 3408ec7

Browse files
committed
feat(error_handling): handle intent and chat errors (#63)
1 parent 6b460ff commit 3408ec7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/ttt/presentation/aiogram/common/routes/error_handling.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,47 @@
1+
from typing import cast
2+
13
from aiogram import Router
4+
from aiogram.exceptions import TelegramBadRequest
5+
from aiogram.filters import ExceptionTypeFilter
26
from aiogram.types import ErrorEvent
7+
from aiogram_dialog import DialogManager, StartMode
8+
from aiogram_dialog.api.exceptions import OutdatedIntent, UnknownIntent
39
from dishka.integrations.aiogram import FromDishka, inject
410
from structlog.types import FilteringBoundLogger
511

612
from ttt.infrastructure.structlog.logger import unexpected_error_log
13+
from ttt.presentation.aiogram_dialog.main_dialog.common import MainDialogState
714

815

916
error_handling_router = Router(name=__name__)
1017

1118

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+
1245
@error_handling_router.error()
1346
@inject
1447
async def _(

0 commit comments

Comments
 (0)