If I want to stop a handler to return some error message ,maybe in middleware,or not.
Just like python's tornado,it has a finish()
method,which can stop handler immediately.
In this case,it doesn't work:
func GetCurrentUserID(c *gin.Context) uint {
c.Done()
c.Abort()
c.AbortWithStatus(999)
c.AbortWithError(999, errors.New("ERR"))
c.AbortWithStatus(777)
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"Message": "Unauthorized"})
return 0
}
Comment From: buzai
@weihaipy done is error, c.Abort() is right
Comment From: crodwell
c.Abort() stops the handler execution chain, the only way to stop the current handler is to return from it. Unfortunately its not possible using any method attached to the context.
Comment From: quocnm102
I see doc about abortWithError, this func return a err and add error to c.Errors
. but, abort() is called inside aboutWithErr will stop execution chains. So c.Errrors
will use for what? and if I have a errorHandling
middleware, it won't working, right?