Repository navigation
fix(api): report validation errors as a readable message - #318
Conversation
TNT-Likely
left a comment
There was a problem hiding this comment.
@Hansen1018 感谢提交!把 422 Pydantic 校验错误从 JSON 数组提取为可读文案(src/web/response.py + tests/test_api_error_messages.py)这个改动很实用。
不过当前分支存在明显的分支叠加与无关改动,暂时无法合并:
- 分支切在了 #315 和 #316 之上,总计夹带了 25 个文件(#315 目前已 Squash 合并进
main); - diff 中包含了一处未在 PR 正文中声明的
.github/workflows/release.yml修改(给workflow_dispatch新增image_name参数),不应混入本 PR。
麻烦基于最新 main 重新开干净分支(只保留 src/web/response.py 和 tests/test_api_error_messages.py),并更新 Tested commit 和隔离验证证据后再通知我们复核。谢谢!
The response wrapper turned a FastAPI validation error list into a JSON string, so the web UI showed the whole Pydantic payload instead of the reason. Take the first error message when the detail is a validation list and keep the rest of the envelope unchanged.
f96ca12 to
c033b7b
Compare
PanWatch contribution checkThe mechanical PR requirements are satisfied. Product acceptance, evidence quality and code review remain maintainer decisions. Scope: 1 production files, 9 added + deleted production lines. Tests, Markdown docs and known lockfiles are excluded. 本检查只核对材料与改动规模,不代表产品或实现已经验收。首次提示后有14天整改窗口;第7天提醒,逾期仍未达标自动关闭。维护者可用 pr:hold 暂停;关闭后仍可重开或提交拆分版本。 |
|
Rebased onto the latest
Validation re-run on the new base: The three failures are the pre-existing, date-dependent ones in For reference, the CI status of this pull request after the rebase:
That failure was addressed by #316 (keep the review API cases inside the 90-day window), which was closed on 2026-12-01. If it is still wanted, it can be reopened — the fix only replaces the pinned date with a date relative to today, so the cases keep asserting the same window behaviour and stop expiring. Until then the |
TNT-Likely
left a comment
There was a problem hiding this comment.
已基于隔离环境完成回归单测(21 passed)与实服 HTTP 协议验证(422 校验报错、positions 自定义校验文案、404 业务 api_error、401 鉴权与 200 成功信封全部符合预期),分支现已非常干净(仅 2 个文件),批准合并!
Background
When a request fails validation, FastAPI answers with a
detaillist of Pydantic errors. The response wrapper turned that list into a JSON string:The web UI shows
messagedirectly in its toast, so a rejected field produced the whole Pydantic payload — a JSON array of error objects — instead of the reason. The frontend could not show anything actionable, and the payload also leaked internal field names and input values.This is visible with the holdings quantity rules: a rejected quantity reported a raw Pydantic list instead of "持仓数量必须大于 0".
Changes
src/web/response.py: when the error detail is a validation list, take the first error'smsgas the message. The rest of the envelope (code,success,data,error_code) is unchanged, and a detail that is not a validation list keeps its previous representation.tests/test_api_error_messages.py: new focused tests for the envelope of a rejected request and for the unchanged success envelope.No request or response field is added, removed or renamed. Only the human-readable
messageof a failed request changes.Scope
One outcome: a rejected request reports a readable reason in the standard error envelope.
Excluded: no change to any validation rule, no change to the error codes, no change to the success envelope, no change to the mobile API.
Validation
Tested commit:
c033b7bCommands and results:
Acceptance evidence
Steps:
POST /api/stockswith"asset_type": "bond".messagefrom the response.Expected:
messageholds the readable reason, not the serialised error list.code,success,dataanderror_codekeep their previous meaning.Observed:
Evidence: the new test file, and the release workflow run of this repository that exercises the same envelope on a real deployment.
Boundaries and risks
detaildictionary withcode/message) keep the previous behaviour, and successful responses are untouched.Follow-up
None.