Little fixes, add exception handler
This commit is contained in:
@@ -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",
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
Reference in New Issue
Block a user