I have post request middleware that looks at presence of errors on the context. And is there are errors, I'm creating specific JSON response that complies with our defined contract.
It mostly works for everything as I use c.Abort() method across my application.
I'm trying to use BingJSON method to parse request body, and when it fails it will write 400 error code onto response and prevents me from changing response header later.
Here is where it is happening:
|
c.AbortWithError(400, err).SetType(ErrorTypeBind) |
For now I have implemented helper function that does safe bind for me:
func SafeJSONBind(obj interface{},c *gin.Context) error {
if err := binding.JSON.Bind(c.Request, obj); err != nil {
return err
}
return nil
}
But it would be nice to have option to perform bind that does c.Abort() instead of c.AbortWithError()