Open
Description
Issue Description
In earlier versions of echo, binding errors were serialized by DefaultHTTPErrorHandler
into JSON. This has apparently changed with the commit fbfe216 fix(DefaultHTTPErrorHandler): return error message when message is an error because the error gets converted into a plain string message and included the "message"
field here:
Line 452 in de44c53
The following example shows that a binding error no longer returns a structured error response.
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"github.com/labstack/echo/v4"
)
func GET(handler echo.HandlerFunc, target string) *httptest.ResponseRecorder {
e := echo.New()
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, target, nil)
c := e.NewContext(req, rec)
err := handler(c)
if err != nil {
e.DefaultHTTPErrorHandler(err, c)
}
return rec
}
func main() {
handler := func(c echo.Context) error {
var docNum int
err := echo.PathParamsBinder(c).
MustInt("docNum", &docNum).
BindError()
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
return c.JSON(http.StatusOK, map[string]interface{}{"received": docNum})
}
resp := GET(handler, "/doc/1234")
fmt.Println(resp.Body.String())
}
With v4.10.2
:
➜ go run main.go
{"field":"docNum","message":"required field value is empty"}
With v4.13.3
:
➜ go run main.go
{"message":"code=400, message=required field value is empty, field=docNum"}
If this change was on purpose and there is no aim to serialize binding errors, in this case the json tag in BindingError
seems to be superfluous.
Line 71 in de44c53
Metadata
Metadata
Assignees
Labels
No labels