debug: 增强 JSON 解析错误的调试信息
改进: - 记录接收到的请求体大小 - JSON 解析失败时返回详细错误信息 - 返回解析错误详情、body 大小、前 200 字符预览 - 在日志中记录前 500 字符,方便排查问题
This commit is contained in:
+12
-1
@@ -157,11 +157,22 @@ http {
|
||||
return
|
||||
end
|
||||
|
||||
-- 记录请求体大小,方便调试
|
||||
ngx.log(ngx.INFO, "Received body size: ", string.len(body))
|
||||
|
||||
-- 解析 JSON
|
||||
local ok, data = pcall(cjson.decode, body)
|
||||
if not ok then
|
||||
-- 记录解析错误的详细信息
|
||||
ngx.log(ngx.ERR, "JSON parse error: ", data)
|
||||
ngx.log(ngx.ERR, "Body preview (first 500 chars): ", string.sub(body, 1, 500))
|
||||
ngx.status = 400
|
||||
ngx.say(cjson.encode({error = "Invalid JSON"}))
|
||||
ngx.say(cjson.encode({
|
||||
error = "Invalid JSON",
|
||||
details = tostring(data),
|
||||
body_size = string.len(body),
|
||||
body_preview = string.sub(body, 1, 200)
|
||||
}))
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user