From 46c5d3cc051411e229a44742eb19918d6605b1ce Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 12 Jun 2026 16:51:47 +0800 Subject: [PATCH] fix(web): guard MAC client None + filter _raw bytes from Ex responses - get_mac_client() now raises TdxConnectionError (503) when MAC client is None, matching get_ex_client() behavior. Previously returned None causing AttributeError (500) on all 12 MAC endpoints. - _records_to_df_resp() filters out internal _raw: bytes fields from Ex dataclass models. Previously asdict() included binary protocol data that is not JSON-serializable and would cause 500 errors. Co-Authored-By: Claude --- src/easy_tdx/web/deps.py | 6 +++++- src/easy_tdx/web/routers/ex_market.py | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/easy_tdx/web/deps.py b/src/easy_tdx/web/deps.py index a1b8386..a121205 100644 --- a/src/easy_tdx/web/deps.py +++ b/src/easy_tdx/web/deps.py @@ -17,7 +17,11 @@ def get_client(request: Request) -> AsyncTdxClient: def get_mac_client(request: Request) -> Any: """从 app.state 获取共享的 AsyncMacClient 实例。""" - client: Any = request.app.state.mac_client + from easy_tdx.exceptions import TdxConnectionError + + client: Any | None = request.app.state.mac_client + if client is None: + raise TdxConnectionError("MAC 客户端未连接") return client diff --git a/src/easy_tdx/web/routers/ex_market.py b/src/easy_tdx/web/routers/ex_market.py index 09de9a0..fb92b0f 100644 --- a/src/easy_tdx/web/routers/ex_market.py +++ b/src/easy_tdx/web/routers/ex_market.py @@ -15,12 +15,13 @@ router = APIRouter(tags=["ex-market"]) def _records_to_df_resp(records: list[Any]) -> DataFrameResponse: - """将 dataclass 列表转为 DataFrameResponse。""" + """将 dataclass 列表转为 DataFrameResponse(过滤内部 _raw 字段)。""" import pandas as pd if not records: return DataFrameResponse(data=[], count=0) - df = pd.DataFrame([asdict(r) for r in records]) + rows = [{k: v for k, v in asdict(r).items() if not k.startswith("_")} for r in records] + df = pd.DataFrame(rows) return DataFrameResponse.from_dataframe(df)