auto creating error with code in ghttp.MiddlewareHandlerResponse when there's no 200 http status code (#2223)

This commit is contained in:
John Guo 2022-10-20 15:50:17 +08:00 committed by GitHub
parent 038548c188
commit ee6103418b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,19 +40,25 @@ func MiddlewareHandlerResponse(r *Request) {
code = gcode.CodeInternalError
}
msg = err.Error()
} else if r.Response.Status > 0 && r.Response.Status != http.StatusOK {
msg = http.StatusText(r.Response.Status)
switch r.Response.Status {
case http.StatusNotFound:
code = gcode.CodeNotFound
case http.StatusForbidden:
code = gcode.CodeNotAuthorized
default:
code = gcode.CodeUnknown
}
} else {
code = gcode.CodeOK
if r.Response.Status > 0 && r.Response.Status != http.StatusOK {
msg = http.StatusText(r.Response.Status)
switch r.Response.Status {
case http.StatusNotFound:
code = gcode.CodeNotFound
case http.StatusForbidden:
code = gcode.CodeNotAuthorized
default:
code = gcode.CodeUnknown
}
// It creates error as it can be retrieved by other middlewares.
err = gerror.NewCode(code, msg)
r.SetError(err)
} else {
code = gcode.CodeOK
}
}
r.Response.WriteJson(DefaultHandlerResponse{
Code: code.Code(),
Message: msg,