From 65e312359f1b917f5b9d36c62c3e3d52098a05af Mon Sep 17 00:00:00 2001 From: Vadim Goncharov Date: Fri, 27 Jan 2023 15:36:34 +0300 Subject: [PATCH] feat: Add drop state to internal error handler --- app/bot/commands/{% if CI %}test.py{% endif %} | 15 +++++++++++++-- app/bot/error_handlers/internal_error.py | 4 ++++ tests/commands/{% if CI %}test_test.py{% endif %} | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/app/bot/commands/{% if CI %}test.py{% endif %} b/app/bot/commands/{% if CI %}test.py{% endif %} index 280e920..288f03d 100644 --- a/app/bot/commands/{% if CI %}test.py{% endif %} +++ b/app/bot/commands/{% if CI %}test.py{% endif %} @@ -1,12 +1,15 @@ """Handlers for tests.""" -from pybotx import ( +from unittest.mock import AsyncMock + +from pybotx import ( # noqa: WPS235 AttachmentTypes, Bot, BotShuttingDownError, BubbleMarkup, HandlerCollector, IncomingMessage, + IncomingMessageHandlerFunc, KeyboardMarkup, OutgoingMessage, ) @@ -17,7 +20,15 @@ from app.bot.middlewares.db_session import db_session_middleware from app.db.record.repo import RecordRepo from app.services.answer_error import AnswerError, AnswerMessageError -collector = HandlerCollector() + +async def fsm_middleware( + message: IncomingMessage, bot: Bot, call_next: IncomingMessageHandlerFunc +) -> None: + message.state.fsm = AsyncMock() + await call_next(message, bot) + + +collector = HandlerCollector(middlewares=[fsm_middleware]) @collector.command("/_test-redis-callback-repo", visible=False) diff --git a/app/bot/error_handlers/internal_error.py b/app/bot/error_handlers/internal_error.py index 5d14cb3..7978530 100644 --- a/app/bot/error_handlers/internal_error.py +++ b/app/bot/error_handlers/internal_error.py @@ -13,6 +13,10 @@ async def internal_error_handler( error_uuid = uuid4() logger.exception(f"Internal error {error_uuid}:") + fsm_manager = getattr(message.state, "fsm", None) + if fsm_manager: + await fsm_manager.drop_state() + is_bot_active = not isinstance(exc, BotShuttingDownError) await bot.answer_message( strings.SOMETHING_GOES_WRONG.format(error_uuid=error_uuid), diff --git a/tests/commands/{% if CI %}test_test.py{% endif %} b/tests/commands/{% if CI %}test_test.py{% endif %} index 710f196..17b6424 100644 --- a/tests/commands/{% if CI %}test_test.py{% endif %} +++ b/tests/commands/{% if CI %}test_test.py{% endif %} @@ -362,6 +362,7 @@ async def test_fail_handler( await bot.async_execute_bot_command(message) # - Assert - + message.state.fsm.drop_state.assert_awaited_once() assert re.search(r"Internal error ([a-z\d\-]*)", loguru_caplog.text) assert re.search( (