mirror of
https://github.com/ZhuLinsen/daily_stock_analysis
synced 2026-09-20 10:53:33 +08:00
* fix(api): return json from root health endpoint * test(web): stabilize vitest runtime setup * chore(security): warn on public web exposure without auth * refactor(api): centralize error response helpers * chore(db): record baseline schema version * refactor(web): extract portfolio formatting helpers * fix(db): make schema baseline initialization idempotent * docs: document health check and schema baseline changes
34 lines
912 B
Python
34 lines
912 B
Python
# -*- coding: utf-8 -*-
|
|
"""Shared helpers for API error responses."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from fastapi import HTTPException
|
|
from fastapi.responses import JSONResponse
|
|
|
|
|
|
def error_body(error: str, message: str, *, detail: Any = None) -> dict[str, Any]:
|
|
body: dict[str, Any] = {
|
|
"error": error,
|
|
"message": message,
|
|
}
|
|
if detail is not None:
|
|
body["detail"] = detail
|
|
return body
|
|
|
|
|
|
def api_error(status_code: int, error: str, message: str, *, detail: Any = None) -> HTTPException:
|
|
return HTTPException(
|
|
status_code=status_code,
|
|
detail=error_body(error, message, detail=detail),
|
|
)
|
|
|
|
|
|
def error_json_response(status_code: int, error: str, message: str, *, detail: Any = None) -> JSONResponse:
|
|
return JSONResponse(
|
|
status_code=status_code,
|
|
content=error_body(error, message, detail=detail),
|
|
)
|