Little fixes, add exception handler

This commit is contained in:
2026-01-24 21:55:27 +03:00
parent 7758a3cf62
commit 239dd05992
14 changed files with 244 additions and 52 deletions

View File

@@ -1,5 +1,6 @@
from .auth import AuthMiddleware
from .depends import DependsMiddleware
from .exception_handler import ExceptionHandlerMiddleware
from .typing import TypingMiddleware
@@ -8,6 +9,8 @@ __all__ = (
"AuthMiddleware",
# depends
"DependsMiddleware",
# exception_handler,
"ExceptionHandlerMiddleware",
# typing
"TypingMiddleware",
)

View File

@@ -0,0 +1,31 @@
import traceback
from typing import Any, Awaitable, Callable
import aiogram
from aiogram import types
from aiogram.enums import ParseMode
from birthday_pool_bot.telegram_bot.ui import constants
class ExceptionHandlerMiddleware(aiogram.BaseMiddleware):
async def __call__(
self,
handler: Callable[[types.TelegramObject, dict[str, Any]], Awaitable[Any]],
event: types.TelegramObject,
data: dict[str, Any],
):
try:
await handler(event, data)
except Exception:
traceback.print_exc()
if isinstance(event, types.Message):
await event.reply(
text=constants.ERROR_MESSAGE,
parse_mode=ParseMode.MARKDOWN,
)
elif isinstance(event, types.CallbackQuery) is not None:
await event.answer(
text=constants.ERROR_MESSAGE,
parse_mode=ParseMode.MARKDOWN,
)