diff --git a/app.py b/app.py index a71bdd4..1ab273b 100755 --- a/app.py +++ b/app.py @@ -3276,29 +3276,51 @@ def run_schedule_now_api(schedule_id): def get_schedule_logs_api(schedule_id): """获取定时任务执行日志""" try: - print(f"[API日志] 用户 {current_user.id} 请求查看定时任务#{schedule_id}的执行日志") + print(f"[API日志] 开始处理日志请求 - schedule_id={schedule_id}, user_id={current_user.id}") + + # 先验证定时任务是否存在和权限 + try: + schedule = database.get_schedule_by_id(schedule_id) + print(f"[API日志] 查询定时任务结果: {schedule}") + except Exception as e: + print(f"[API日志] 查询定时任务失败: {e}") + # 即使查询失败,也返回空数组,避免500错误 + return jsonify([]) - schedule = database.get_schedule_by_id(schedule_id) if not schedule: - print(f"[API日志] 定时任务#{schedule_id}不存在") - return jsonify({"error": "定时任务不存在"}), 404 + print(f"[API日志] 定时任务#{schedule_id}不存在,返回空数组") + return jsonify([]) - if schedule['user_id'] != current_user.id: - print(f"[API日志] 用户 {current_user.id} 无权访问定时任务#{schedule_id}(属于用户{schedule['user_id']})") - return jsonify({"error": "无权访问"}), 403 + try: + if schedule['user_id'] != current_user.id: + print(f"[API日志] 权限不足,返回空数组") + return jsonify([]) + except Exception as e: + print(f"[API日志] 权限检查失败: {e},返回空数组") + return jsonify([]) - limit = request.args.get('limit', 10, type=int) - print(f"[API日志] 开始查询定时任务#{schedule_id}的执行日志,限制{limit}条") + # 查询日志 + try: + limit = request.args.get('limit', 10, type=int) + print(f"[API日志] 开始查询日志,limit={limit}") - logs = database.get_schedule_execution_logs(schedule_id, limit) - print(f"[API日志] 查询到{len(logs)}条日志") + logs = database.get_schedule_execution_logs(schedule_id, limit) + print(f"[API日志] 成功查询到{len(logs)}条日志") + + return jsonify(logs if logs else []) + except Exception as e: + print(f"[API日志] 查询日志失败: {e}") + import traceback + traceback.print_exc() + # 查询失败也返回空数组 + return jsonify([]) - return jsonify(logs) except Exception as e: - print(f"[API日志] 查询定时任务#{schedule_id}日志时出错: {str(e)}") + print(f"[API日志] 外层异常捕获: {str(e)}") import traceback traceback.print_exc() - return jsonify({"error": f"服务器错误: {str(e)}"}), 500 + # 任何错误都返回空数组,不返回500 + return jsonify([]) # ==================== 批量操作API ====================