ZhuLinsen/daily_stock_analysis · error · HTTPException

not_found

not_found

Error message

未找到股票 {stock_code} 的行情数据

What it means

Raised by GET /{stock_code}/quote in api/v1/endpoints/stocks.py when StockService.get_realtime_quote(stock_code) returns None. The endpoint executes the service call in FastAPI's threadpool (def, not async def) and maps a None result to a 404 with error code 'not_found'. It means the service layer resolved no quote data for the requested symbol, not that the HTTP layer failed.

Source

Thrown at api/v1/endpoints/stocks.py:441

    获取指定股票的最新行情数据
    
    Args:
        stock_code: 股票代码(如 600519、00700、AAPL)
        
    Returns:
        StockQuote: 实时行情数据
        
    Raises:
        HTTPException: 404 - 股票不存在
    """
    try:
        service = StockService()
        
        # 使用 def 而非 async def,FastAPI 自动在线程池中执行
        result = service.get_realtime_quote(stock_code)
        
        if result is None:
            raise HTTPException(
                status_code=404,
                detail={
                    "error": "not_found",
                    "message": f"未找到股票 {stock_code} 的行情数据"
                }
            )
        
        return StockQuote(
            stock_code=result.get("stock_code", stock_code),
            stock_name=result.get("stock_name"),
            current_price=result.get("current_price", 0.0),
            change=result.get("change"),
            change_percent=result.get("change_percent"),
            open=result.get("open"),
            high=result.get("high"),
            low=result.get("low"),
            prev_close=result.get("prev_close"),
            volume=result.get("volume"),

View on GitHub (pinned to 5159bd72e8)

Solutions

  1. Verify the stock code format matches the supported markets (A-share 6-digit, hk-prefixed, or US ticker) and retry
  2. Test the same code through another endpoint (e.g. /{stock_code}/history) to confirm the symbol is recognized at all
  3. Check the data provider logs/config (data_provider fallback chain) if a known-good code still returns 404
  4. If the code is valid, inspect StockService.get_realtime_quote to see which provider returning None swallows the real upstream error

Example fix

// before
const res = await fetch(`/api/v1/stocks/${code}/quote`);
const data = await res.json(); // crashes on 404
// after
const res = await fetch(`/api/v1/stocks/${code}/quote`);
if (res.status === 404) {
  throw new Error(`No quote for ${code}: check the symbol format`);
}
const data = await res.json();
Defensive patterns

Strategy: validation

Validate before calling

function isValidStockCode(code) {
  return /^(\d{6}|hk\d{4,5}|[A-Za-z]{1,6})$/.test(code);
}
// call before GET /{stock_code}/quote

Type guard

function isStockQuote(d) {
  return d != null && typeof d.stock_code === 'string' && typeof d.current_price === 'number';
}

Try / catch

if (res.status === 404 && body.error === 'not_found') { /* treat as unknown symbol, do not retry */ }

Prevention

When it happens

Trigger: Calling GET /api/v1/stocks/{stock_code}/quote with a malformed or nonexistent stock code (e.g. '60051', 'XXXXXX', a delisted symbol), or a valid code whose quote fetch fails upstream in a way that yields None instead of raising.

Common situations: Typos in stock codes; using the wrong market prefix format (the repo supports A-share like 600519, HK like hk00700, and US like AAPL); querying during hours when the data provider returns nothing; data-source fallback returning None after all providers fail.

Related errors


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