Enhance private handlers structure and add database support
- Introduced a new `PrivateHandlers` class to encapsulate private message handling logic, improving organization and maintainability. - Added new dependencies in `requirements.txt` for database support with `aiosqlite`. - Updated the private handlers to utilize modular components for better separation of concerns and easier testing. - Implemented error handling and logging for improved robustness in message processing.
This commit is contained in:
29
helper_bot/handlers/private/decorators.py
Normal file
29
helper_bot/handlers/private/decorators.py
Normal file
@@ -0,0 +1,29 @@
|
||||
"""Decorators and utility functions for private handlers"""
|
||||
|
||||
import traceback
|
||||
from aiogram import types
|
||||
from logs.custom_logger import logger
|
||||
|
||||
|
||||
def error_handler(func):
|
||||
"""Decorator for centralized error handling"""
|
||||
async def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return await func(*args, **kwargs)
|
||||
except Exception as e:
|
||||
logger.error(f"Error in {func.__name__}: {str(e)}")
|
||||
# Try to send error to logs if possible
|
||||
try:
|
||||
message = next((arg for arg in args if isinstance(arg, types.Message)), None)
|
||||
if message and hasattr(message, 'bot'):
|
||||
from helper_bot.utils.base_dependency_factory import get_global_instance
|
||||
bdf = get_global_instance()
|
||||
important_logs = bdf.settings['Telegram']['important_logs']
|
||||
await message.bot.send_message(
|
||||
chat_id=important_logs,
|
||||
text=f"Произошла ошибка в {func.__name__}: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
|
||||
)
|
||||
except:
|
||||
pass # If we can't log the error, at least it was logged to logger
|
||||
raise
|
||||
return wrapper
|
||||
Reference in New Issue
Block a user