mirror of
https://github.com/infinite-iroha/touka.git
synced 2026-06-13 15:47:38 +08:00
fix: reduce default error response overhead
Encode the built-in 404 and 405 payload with a fixed struct instead of a map so default error pages allocate less on the hot miss path. Add a regression test to keep the JSON shape stable.
This commit is contained in:
parent
57847fa446
commit
fa027347d3
2 changed files with 28 additions and 5 deletions
12
engine.go
12
engine.go
|
|
@ -126,6 +126,12 @@ type ErrorHandler func(c *Context, code int, err error)
|
|||
var errMethodNotAllowed = errors.New("method not allowed")
|
||||
var errNotFound = errors.New("not found")
|
||||
|
||||
type defaultErrorResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
var methodNotAllowedHandler HandlerFunc = func(c *Context) {
|
||||
httpMethod := c.Request.Method
|
||||
requestPath := routeLookupPath(c.Request)
|
||||
|
|
@ -187,11 +193,7 @@ func defaultErrorHandle(c *Context, code int, err error) { // 检查客户端是
|
|||
if err != nil {
|
||||
errMsg = err.Error()
|
||||
}
|
||||
c.JSON(code, H{
|
||||
"code": code,
|
||||
"message": http.StatusText(code),
|
||||
"error": errMsg,
|
||||
})
|
||||
c.JSON(code, defaultErrorResponse{Code: code, Message: http.StatusText(code), Error: errMsg})
|
||||
c.Writer.Flush()
|
||||
c.Abort()
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue