Skip to content

Commit

Permalink
refactor: improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
j3rrryy committed Jan 5, 2025
1 parent 37e62b9 commit be434e3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 0 additions & 2 deletions src/database/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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
19 changes: 13 additions & 6 deletions src/handlers/user_handlers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logging

from vkbottle.bot import BotLabeler, Message

from src.database import create_user, get_top, get_user, update_rating
Expand All @@ -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)

Expand All @@ -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()
)
Expand Down Expand Up @@ -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"])


Expand Down Expand Up @@ -103,15 +108,15 @@ 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"])


@user_labeler.private_message(text=KB_LEXICON_RU["solve"])
@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()
)
Expand All @@ -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"])


Expand All @@ -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"])


Expand Down

0 comments on commit be434e3

Please sign in to comment.