🐛 fix(handlers): 统一错误响应为安全 JSON 并补参数校验

- 新增 respondError:JSON 安全编码,客户端错误返回 400,服务端错误返回 500
- 收编 book/content/media/category 中 13 处 fmt.Fprintf 内联 JSON,修复错误信息含引号/换行时产出非法 JSON 的问题
- search/detail/catalog/multi-detail/content/multi-content/item-summary/audio/video 补必需参数校验,缺参返回 400
- search 的 offset/tab_type 非数字时返回 400,不再静默置 0
- 补齐 multi-detail/multi-content 的请求统计计数
- 新增 error_response_test.go:3 个测试锁定错误响应行为
- openapi.yaml 新增 ServerError 响应组件,15 个业务接口补 400/500 响应定义

BREAKING CHANGE: 业务接口错误不再返回 HTTP 200,缺参返回 400,上游失败返回 500
This commit is contained in:
chenxin committed 2026-09-29 22:47:01 +08:00
1 parent 1eccebedf9
commit d1774a3902
7 files changed
+209 -52

No files matched your search

+2 -2
View File
@@ -15,7 +15,7 @@ func HandleCategoryFront(w http.ResponseWriter, r *http.Request) {
result, err := fqapi.HandleCategoryFront(tab)
if err != nil {
fmt.Fprintf(w, `{"code": -1, "msg": "获取分类列表失败: %s"}`, err.Error())
respondError(w, "获取分类列表失败: "+err.Error(), false)
return
}
@@ -35,7 +35,7 @@ func HandleCategoryLanding(w http.ResponseWriter, r *http.Request) {
result, err := fqapi.HandleCategoryLanding(categoryID, offset, wordNumber, bookStatus, sortBy)
if err != nil {
fmt.Fprintf(w, `{"code": -1, "msg": "获取分类详情失败: %s"}`, err.Error())
respondError(w, "获取分类详情失败: "+err.Error(), false)
return
}