context.AbortWithError should be renamed context.AbortWithStatusError. And AbortWithError should not have code param.
func (c *Context) AbortWithError(err error) *Error {
c.Abort()
return c.Error(err)
}
Use the AbortWithError we can process err in middleware like this:
func err_process(c *gin.Context) {
c.Next()
err := c.Errors.Last()
if err != nil && !c.Writer.Written() {
c.JSON(http.StatusInternalServerError, gin.H{
"error_msg": err.Error(),
})
}
}
Otherwise if we can't write header in middleware err_process.
Comment From: charlie-wasp
@appleboy was this issue ever considered? Or maybe it should be closed as "won't fix"?
Comment From: appleboy
@thinkerou What do you think?
Comment From: YamiOdymel
Though it really needs a fix, but it also breaks backward compatibility.
AbortWithError
with a status parameter prevents manual control over the output in an error-handling middleware because the status in the header has already been written by AbortWithError
. :(