fix: catch SD errors in background task and send universal error message

This commit is contained in:
Денис Кривоченко
2026-07-30 23:24:04 +07:00
parent 81714f4bc2
commit 996cfc7ad3
+10 -7
View File
@@ -219,7 +219,6 @@ def _extract_attachments_from_content(content, is_attachment_type=False):
async def _create_ticket_and_attach_files(user_id, msg_text, session, msg, login): async def _create_ticket_and_attach_files(user_id, msg_text, session, msg, login):
"""Логика генерации темы, отправки в SD и загрузки всех очередей вложений.""" """Логика генерации темы, отправки в SD и загрузки всех очередей вложений."""
session["step"] = "creating_ticket" session["step"] = "creating_ticket"
try:
ad_user = await asyncio.to_thread(get_ad_user_sync, login) ad_user = await asyncio.to_thread(get_ad_user_sync, login)
sd_workflow_logger.info(f"👤 [AD Lookup] User: {login} -> Found: {ad_user is not None}") sd_workflow_logger.info(f"👤 [AD Lookup] User: {login} -> Found: {ad_user is not None}")
sender_email = ad_user.get("mail") if ad_user else DEFAULT_REQUESTER sender_email = ad_user.get("mail") if ad_user else DEFAULT_REQUESTER
@@ -260,11 +259,6 @@ async def _create_ticket_and_attach_files(user_id, msg_text, session, msg, login
else: else:
session["step"] = "need_text" session["step"] = "need_text"
await msg.answer(system_error_text(EMOJI_DIGITS), parse_mode="html") await msg.answer(system_error_text(EMOJI_DIGITS), parse_mode="html")
except Exception as e:
logger.exception(f"Ошибка: {e}")
sd_workflow_logger.error(f"❌ [SD Error] {e}")
session["step"] = "need_text"
await msg.answer(system_error_text(EMOJI_DIGITS), parse_mode="html")
async def _wait_for_attachments_and_create(user_id, session): async def _wait_for_attachments_and_create(user_id, session):
"""Фоновый таймер с возможностью отмены (Debounce).""" """Фоновый таймер с возможностью отмены (Debounce)."""
@@ -280,7 +274,16 @@ async def _wait_for_attachments_and_create(user_id, session):
msg = session.get("msg") msg = session.get("msg")
login = session.get("login") login = session.get("login")
if msg: if msg:
await _create_ticket_and_attach_files(user_id, session["msg_text"], session, msg, login) try:
await _create_ticket_and_attach_files(user_id, session["msg_text"], session, msg, login)
except Exception as e:
logger.exception(f"❌ [SD Ticket Failed] user={user_id}: {e}")
sd_workflow_logger.error(f"❌ [SD Ticket] {e}")
session["step"] = "need_text"
try:
await msg.answer(system_error_text(EMOJI_DIGITS), parse_mode="html")
except Exception:
sd_workflow_logger.error(f"❌ [SD] Failed to send error message to {user_id}")
@router.message() @router.message()
async def sd_module_handler(msg: Message): async def sd_module_handler(msg: Message):