ZhuLinsen/daily_stock_analysis · error · HTTPException

internal_error

internal_error

Error message

回测执行失败: {str(exc)}

What it means

POST /backtest/run maps any non-ValueError, non-HTTPException exception to HTTP 500 code=internal_error with message '回测执行失败: {str(exc)}' (backtest.py:79-86). This is the catch-all failure path of the run endpoint: database errors from db_manager, unexpected service bugs, or environment problems inside BacktestService.run_backtest all land here. The original traceback is logged server-side with exc_info=True.

Source

Thrown at api/v1/endpoints/backtest.py:82

            code=request.code,
            force=request.force,
            eval_window_days=request.eval_window_days,
            min_age_days=request.min_age_days,
            analysis_date_from=request.analysis_date_from,
            analysis_date_to=request.analysis_date_to,
            limit=request.limit,
        )
        return BacktestRunResponse(**stats)
    except ValueError as exc:
        raise HTTPException(
            status_code=400,
            detail={"error": "invalid_params", "message": str(exc)},
        )
    except HTTPException:
        raise
    except Exception as exc:
        logger.error(f"回测执行失败: {exc}", exc_info=True)
        raise HTTPException(
            status_code=500,
            detail={"error": "internal_error", "message": f"回测执行失败: {str(exc)}"},
        )


@router.get(
    "/results",
    response_model=BacktestResultsResponse,
    responses={
        200: {"description": "回测结果列表"},
        400: {"description": "请求参数错误", "model": ErrorResponse},
        500: {"description": "服务器错误", "model": ErrorResponse},
    },
    summary="获取回测结果",
    description="分页获取回测结果,支持按股票代码过滤",
)
def get_backtest_results(
    code: Optional[str] = Query(None, description="股票代码筛选"),

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Read the server log for the logged traceback (logger.error ... exc_info=True) — the HTTP message's str(exc) is usually too short to diagnose
  2. Reproduce with a narrower request: single code, small limit, tight date window, to bisect whether the failure is data-specific
  3. Verify DB connectivity/migrations (the same db_manager other endpoints use) and retry once transient locks clear
  4. If it reproduces deterministically, capture code+params+traceback and report it as a bug — there is no client-side fix for this path
Defensive patterns

Strategy: fallback

Try / catch

try:
    stats = post_backtest_run(payload)
except HTTPError as e:
    if e.response.status_code == 500 and '回测执行失败' in e.response.text:
        stats = post_backtest_run(narrowed_payload)  # single code, small limit
        if stats is None:
            report_with_server_traceback(payload)
    else:
        raise

Prevention

When it happens

Trigger: Database unavailable or schema mismatch when the service writes/reads backtest records; a bug in the backtest engine for the specific stock/date window; data-provider exceptions that escape the service's own handling; OutOfMemory/timeout-style failures during a large run.

Common situations: SQLite file locked by a concurrent long run; DB not migrated after upgrading the backtest schema; a single pathological stock whose stored analysis rows break an assumption in the aggregation code; resource exhaustion when limit is large.

Related errors


AI-assisted analysis of ZhuLinsen/daily_stock_analysis@5159bd72e8 (2026-08-15). Data as JSON: /api/errors/e98bdcb2ce3419e4. Report an issue: GitHub.