- With issues:
- Use the search tool before opening a new issue.
- Please provide source code and commit sha if you found a bug.
- Review existing issues and provide feedback or react to them.
Description
How to reproduce
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
g := gin.Default()
g.GET("/hello/:name", func(c *gin.Context) {
if true {
c.AbortWithStatusJSON(200, gin.H{"hello": "world"})
}
panic("context should be aborted")
})
g.Run(":9000")
}
Expectations
$ curl http://localhost:9000/hello/world
{ "hello": "world" }
Actual result
$ curl -i http://localhost:9000/hello/world
{ "hello": "world" }
// panic chain...
it's under my impression that with a name that incluced Abort that it's aborted in order to do what I need to do then I need to include a return
after the c.AbortWithContext()
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
g := gin.Default()
g.GET("/hello/:name", func(c *gin.Context) {
if true {
c.AbortWithStatusJSON(200, gin.H{"hello": "world"})
return
}
panic("context should be aborted")
})
g.Run(":9000")
}
Environment
- go version: 1.21
- gin version (or commit ref): 1.9.1
- operating system: Mac OS M1 Sanoma
Comment From: cgarm
In Go, for a function to interrupt its execution from another function, the called function must call os.Exit or panic. Call AbortWithStatusJSON to ensure the remaining handlers for this request are not called.
For example:
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
g := gin.Default()
g.GET("/hello/:name", func(c *gin.Context) {
c.AbortWithStatusJSON(200, gin.H{"hello": "world"})
}, func(c *gin.Context) {
panic("context should be aborted")
})
g.Run(":9000")
}
Comment From: ctfrancia
Ok thank you for informing me