Skip to content

Commit

Permalink
feat: Add drop state to internal error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
vadikball committed Jan 31, 2023
1 parent 815f5c0 commit 65e3123
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/bot/commands/{% if CI %}test.py{% endif %}
Original file line number Diff line number Diff line change
@@ -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,
)
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions app/bot/error_handlers/internal_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions tests/commands/{% if CI %}test_test.py{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
(
Expand Down

0 comments on commit 65e3123

Please sign in to comment.