From be434e32e55dfba7cf1c6793b3d8af09f56153a0 Mon Sep 17 00:00:00 2001 From: j3rrryy Date: Mon, 6 Jan 2025 02:34:45 +0300 Subject: [PATCH] refactor: improve logging --- src/database/crud.py | 2 -- src/handlers/user_handlers.py | 19 +++++++++++++------ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/database/crud.py b/src/database/crud.py index ad3932d..26078bd 100644 --- a/src/database/crud.py +++ b/src/database/crud.py @@ -38,7 +38,6 @@ async def get_user( user = await session.get(User, user_id) return user.columns_to_dict() except Exception as e: - await session.rollback() raise DatabaseError from e @@ -80,5 +79,4 @@ async def get_top(user_id: int, sessionmaker: async_sessionmaker[AsyncSession]) user_pos = higher_ratings + 1 return user_pos except Exception as e: - await session.rollback() raise DatabaseError from e diff --git a/src/handlers/user_handlers.py b/src/handlers/user_handlers.py index 9297aea..8bb6dd8 100644 --- a/src/handlers/user_handlers.py +++ b/src/handlers/user_handlers.py @@ -1,3 +1,5 @@ +import logging + from vkbottle.bot import BotLabeler, Message from src.database import create_user, get_top, get_user, update_rating @@ -16,6 +18,7 @@ from src.middlewares import DatabaseMiddleware from src.services import heads_or_tails_game, math_game +logger = logging.getLogger() user_labeler = BotLabeler() user_labeler.message_view.register_middleware(DatabaseMiddleware) @@ -31,7 +34,8 @@ async def start(message: Message): + LEXICON_RU["start"], keyboard=to_menu_kb().get_json(), ) - except DatabaseError: + except DatabaseError as e: + logger.exception(e) await message.answer( ERROR_LEXICON_RU["database_error"], keyboard=to_menu_kb().get_json() ) @@ -68,7 +72,8 @@ async def profile(message: Message): ) await message.answer(LEXICON_RU["profile"] + rating + position + registered) - except DatabaseError: + except DatabaseError as e: + logger.exception(e) await message.answer(ERROR_LEXICON_RU["database_error"]) @@ -103,7 +108,8 @@ async def heads_or_tails_res(message: Message): LEXICON_RU["lose"] + str(new_rating), keyboard=play_again_kb(0).get_json(), ) - except DatabaseError: + except DatabaseError as e: + logger.exception(e) await message.answer(ERROR_LEXICON_RU["database_error"]) @@ -111,7 +117,6 @@ async def heads_or_tails_res(message: Message): @user_labeler.private_message(payload={"game_type": 1}) async def solve_equation(message: Message): eq = math_game() - await message.answer( LEXICON_RU["solve"] + eq[0], keyboard=choose_answer(eq[1]).get_json() ) @@ -127,7 +132,8 @@ async def solve_equation_res_1(message: Message): LEXICON_RU["correct"] + str(new_rating), keyboard=play_again_kb(1).get_json(), ) - except DatabaseError: + except DatabaseError as e: + logger.exception(e) await message.answer(ERROR_LEXICON_RU["database_error"]) @@ -140,7 +146,8 @@ async def solve_equation_res_2(message: Message): await message.answer( LEXICON_RU["wrong"] + str(new_rating), keyboard=play_again_kb(1).get_json() ) - except DatabaseError: + except DatabaseError as e: + logger.exception(e) await message.answer(ERROR_LEXICON_RU["database_error"])