Files
duty-teller/duty_teller/handlers/errors.py
Nikolay Tatarinov ae21883e1e
All checks were successful
CI / lint-and-test (push) Successful in 20s
refactor: streamline configuration loading and enhance admin checks
- Refactored the configuration loading in `config.py` to utilize a single source of truth through the `Settings` class, improving maintainability and clarity.
- Introduced the `is_admin_for_telegram_user` function in `repository.py` to centralize admin checks based on usernames and phone numbers.
- Updated command handlers to use the new admin check function, ensuring consistent access control across the application.
- Enhanced error handling in the `error_handler` to log exceptions when sending error replies to users, improving debugging capabilities.
- Improved the handling of user phone updates in `repository.py` to ensure proper normalization and validation of phone numbers.
2026-02-20 16:42:41 +03:00

31 lines
1012 B
Python

"""Global error handler: log exception and notify user."""
import logging
from telegram import Update
from telegram.ext import ContextTypes
import duty_teller.config as config
from duty_teller.i18n import get_lang, t
logger = logging.getLogger(__name__)
async def error_handler(
update: Update | None, context: ContextTypes.DEFAULT_TYPE
) -> None:
"""Global error handler: log exception and reply with generic message if possible.
Args:
update: Update that caused the error (may be None).
context: Callback context.
"""
logger.exception("Exception while handling an update")
if isinstance(update, Update) and update.effective_message:
try:
user = getattr(update, "effective_user", None)
lang = get_lang(user) if user else config.DEFAULT_LANGUAGE
await update.effective_message.reply_text(t(lang, "errors.generic"))
except Exception:
logger.warning("Could not send error reply to user", exc_info=True)