From fb7a75e76e60e8f6765dc87659ca55a40aedb649 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 10 Dec 2025 16:47:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=BA=E5=8C=96=E5=AE=9A=E6=97=B6=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E6=97=A5=E5=BF=97=E6=9F=A5=E8=AF=A2=E7=9A=84=E9=94=99?= =?UTF-8?q?=E8=AF=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加多层错误保护: 1. 在整个查询函数外层添加try-except 2. 在每行数据处理时也添加try-except 3. 出错时返回空数组而不是抛出异常 4. 打印详细的错误信息和堆栈跟踪 这样即使数据有问题,也不会导致500错误, 而是会正常返回空列表并在日志中显示具体错误。 位置: database.py 第1661-1695行 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- database.py | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/database.py b/database.py index 5f484e9..33a02ae 100755 --- a/database.py +++ b/database.py @@ -1660,27 +1660,39 @@ def update_schedule_execution_log(log_id, **kwargs): def get_schedule_execution_logs(schedule_id, limit=10): """获取定时任务执行日志""" - with db_pool.get_db() as conn: - cursor = conn.cursor() - cursor.execute(''' - SELECT * FROM schedule_execution_logs - WHERE schedule_id = ? - ORDER BY execute_time DESC - LIMIT ? - ''', (schedule_id, limit)) + try: + with db_pool.get_db() as conn: + cursor = conn.cursor() + cursor.execute(''' + SELECT * FROM schedule_execution_logs + WHERE schedule_id = ? + ORDER BY execute_time DESC + LIMIT ? + ''', (schedule_id, limit)) - # 将数据库字段映射到前端期望的字段名 - logs = [] - for row in cursor.fetchall(): - log = dict(row) - # 字段映射 - log['created_at'] = log.get('execute_time') - log['success_count'] = log.get('success_accounts', 0) - log['failed_count'] = log.get('failed_accounts', 0) - log['duration'] = log.get('duration_seconds', 0) - logs.append(log) + # 将数据库字段映射到前端期望的字段名 + logs = [] + rows = cursor.fetchall() - return logs + for row in rows: + try: + log = dict(row) + # 字段映射 + log['created_at'] = log.get('execute_time') + log['success_count'] = log.get('success_accounts', 0) + log['failed_count'] = log.get('failed_accounts', 0) + log['duration'] = log.get('duration_seconds', 0) + logs.append(log) + except Exception as e: + print(f"[数据库] 处理日志行时出错: {e}") + continue + + return logs + except Exception as e: + print(f"[数据库] 查询定时任务日志时出错: {e}") + import traceback + traceback.print_exc() + return [] # 出错时返回空列表,而不是抛出异常 def get_user_all_schedule_logs(user_id, limit=50):