ZhuLinsen/daily_stock_analysis · error · HTTPException

internal_error

internal_error

Error message

查询历史列表失败: {str(e)}

What it means

GET history list catch-all: any unhandled exception while building HistoryListResponse becomes HTTP 500 code=internal_error '查询历史列表失败: {str(e)}' (history.py:290-298). Failures here are infrastructure-level — database errors from HistoryService list queries, or HistoryListItem construction choking on rows with unexpected shapes (schema drift, corrupt rows). The traceback is logged with exc_info=True at history.py:289.

Source

Thrown at api/v1/endpoints/history.py:290

                volume_ratio=item.get("volume_ratio"),
                turnover_rate=item.get("turnover_rate"),
                model_used=item.get("model_used"),
                created_at=item.get("created_at"),
                market_phase_summary=item.get("market_phase_summary"),
            )
            for item in result.get("items", [])
        ]
        
        return HistoryListResponse(
            total=result.get("total", 0),
            page=page,
            limit=limit,
            items=items
        )
        
    except Exception as e:
        logger.error(f"查询历史列表失败: {e}", exc_info=True)
        raise HTTPException(
            status_code=500,
            detail={
                "error": "internal_error",
                "message": f"查询历史列表失败: {str(e)}"
            }
        )


@router.delete(
    "/by-code/{stock_code}",
    response_model=DeleteHistoryResponse,
    responses={
        200: {"description": "删除成功"},
        400: {"description": "股票代码不能为空", "model": ErrorResponse},
        404: {"description": "未找到记录", "model": ErrorResponse},
        500: {"description": "服务器错误", "model": ErrorResponse},
    },
    summary="按股票代码删除历史分析记录",

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Check server logs for the traceback — str(e) in the response is only a hint
  2. Retry with a smaller page/limit and different page numbers to determine whether specific rows are poisonous (page-local 500s indicate corrupt rows)
  3. Verify DB connectivity/migrations and that the DB file/DSN is the one analyses write to
  4. If specific rows are corrupt, identify and re-generate or delete them, then reload the list
Defensive patterns

Strategy: retry

Validate before calling

# Probe with a small page to separate systemic DB failure from corrupt-row failure
import requests

def probe_history(base):
    r = requests.get(f"{base}/api/v1/history", params={'page': 1, 'limit': 5})
    if r.status_code == 500:
        return 'systemic'          # DB down/locked -> retry later
    return 'ok'                    # page-specific corruption -> narrow pages

Try / catch

try:
    data = get_history_list(page, limit)
except HTTPError as e:
    if e.response.status_code == 500 and '查询历史列表失败' in e.response.text:
        if probe_history(BASE) == 'systemic':
            data = get_history_list(page, limit)  # single retry after DB recovers
        else:
            data = get_history_list(page=1, limit=min(limit, 20))  # skip corrupt page
            flag_page_for_cleanup(page)
    else:
        raise

Prevention

When it happens

Trigger: DB unreachable/locked during the list query; a specific page of rows containing malformed entries causing item deserialization to throw; very large offsets/limits triggering DB-side errors; schema changes after upgrade with legacy rows.

Common situations: SQLite lock contention with a running analysis writing history; API version newer than the DB schema (unreadable rows); corrupt row from an interrupted write; page/limit edge values (limit=0 style) reaching the service.

Related errors


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