🐛 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:
1 parent
1eccebedf9
commit
d1774a3902
7 files changed
+209
-52
No files matched your search
+21
-14
@@ -13,13 +13,18 @@ func HandleAudio(w http.ResponseWriter, r *http.Request) {
|
||||
incrementStat("total", "audio")
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
itemIDs := r.URL.Query().Get("item_ids")
|
||||
params, err := validateParams(r, "item_ids")
|
||||
if err != nil {
|
||||
respondError(w, err.Error(), true)
|
||||
return
|
||||
}
|
||||
|
||||
toneID := r.URL.Query().Get("tone_id")
|
||||
output := r.URL.Query().Get("output")
|
||||
|
||||
result, err := fqapi.HandleAudio(itemIDs, toneID, output)
|
||||
result, err := fqapi.HandleAudio(params["item_ids"], toneID, output)
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, `{"code": -1, "msg": "获取音频失败: %s"}`, err.Error())
|
||||
respondError(w, "获取音频失败: "+err.Error(), false)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -62,11 +67,15 @@ func HandleVideo(w http.ResponseWriter, r *http.Request) {
|
||||
incrementStat("total", "video")
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
|
||||
itemIDs := r.URL.Query().Get("item_ids")
|
||||
|
||||
result, err := fqapi.HandleVideo(itemIDs)
|
||||
params, err := validateParams(r, "item_ids")
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, `{"code": -1, "msg": "获取视频失败: %s"}`, err.Error())
|
||||
respondError(w, err.Error(), true)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := fqapi.HandleVideo(params["item_ids"])
|
||||
if err != nil {
|
||||
respondError(w, "获取视频失败: "+err.Error(), false)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -76,14 +85,13 @@ func HandleVideo(w http.ResponseWriter, r *http.Request) {
|
||||
// HandleManga 漫画处理
|
||||
func HandleManga(w http.ResponseWriter, r *http.Request) {
|
||||
incrementStat("total", "manga")
|
||||
|
||||
itemID := r.URL.Query().Get("item_ids")
|
||||
if itemID == "" {
|
||||
itemID = r.URL.Query().Get("item_id")
|
||||
}
|
||||
|
||||
if itemID == "" {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
fmt.Fprint(w, `{"code": -1, "msg": "缺少item_ids参数"}`)
|
||||
respondError(w, "缺少 item_ids 参数", true)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -96,8 +104,7 @@ func HandleManga(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
result, err := fqapi.HandleGetManga(itemID, showHTML, outputMode)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
fmt.Fprintf(w, `{"code": -1, "msg": "获取漫画失败: %s"}`, err.Error())
|
||||
respondError(w, "获取漫画失败: "+err.Error(), false)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -113,16 +120,16 @@ func HandleManga(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprint(w, `<p>未找到漫画内容</p>`)
|
||||
} else {
|
||||
// 返回JSON格式
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
jsonData, err := json.Marshal(map[string]interface{}{
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"data": result,
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Fprintf(w, `{"code": -1, "msg": "JSON编码失败: %s"}`, err.Error())
|
||||
respondError(w, "JSON编码失败: "+err.Error(), false)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
fmt.Fprint(w, string(jsonData))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user