mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
auto creating error with code in ghttp.MiddlewareHandlerResponse when there's no 200 http status code (#2223)
This commit is contained in:
parent
038548c188
commit
ee6103418b
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user