🐛 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
@@ -96,6 +96,15 @@ func StopStatisticsSaver() {
|
||||
}
|
||||
}
|
||||
|
||||
// respondError 统一错误响应(JSON 安全编码;isClientErr 为 true 时返回 400,否则 500)
|
||||
func respondError(w http.ResponseWriter, msg string, isClientErr bool) {
|
||||
status := http.StatusInternalServerError
|
||||
if isClientErr {
|
||||
status = http.StatusBadRequest
|
||||
}
|
||||
respondJSON(w, status, map[string]interface{}{"code": -1, "msg": msg})
|
||||
}
|
||||
|
||||
// RequireAccessKey 验证访问密钥中间件
|
||||
func RequireAccessKey(handler http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
Reference in new issue
Block a user