Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: the result will be incorrect when call gin.Context.Writer.Status() in our custom gin middleware used after this(#46) #47

Closed
zhyee opened this issue Feb 16, 2023 · 2 comments

Comments

@zhyee
Copy link
Contributor

zhyee commented Feb 16, 2023

consider this case:

func Timeout() gin.HandlerFunc {
	return timeout.New(timeout.WithTimeout(time.Second*1),
		timeout.WithHandler(func(ctx *gin.Context) {
			ctx.Next()
		}),
		timeout.WithResponse(func(ctx *gin.Context) {
			ctx.String(http.StatusRequestTimeout, "timeout")
		}),
	)
}

func Logger() gin.HandlerFunc {
	return func(c *gin.Context) {
		c.Next()
		// access the status we are sending
		status := c.Writer.Status() // Bug: in any middleware used after this middleware, the result will always be 200.
		log.Println(status)
	}
}

func main() {
	r := gin.New()
	r.Use(Timeout())
	r.Use(Logger())

	r.GET("/test", func(c *gin.Context) {
		c.Status(http.StatusInternalServerError)
	})
	// Listen and serve on 0.0.0.0:8080
	r.Run(":8080")
}
@zhyee zhyee changed the title Bug: the result will be incorrect when call gin.Context.Writer.Status() in our custom gin middleware used after this Bug: the result will be incorrect when call gin.Context.Writer.Status() in our custom gin middleware used after this(#46) Feb 16, 2023
@fr-wentianqi
Copy link

same issue here, by moving your timeout middleware down underneath the logger middleware will solve the issue

zhyee pushed a commit to GuanceCloud/timeout that referenced this issue Nov 24, 2023
…r.Status() is incorrect in other custom gin middlewares.
@appleboy
Copy link
Member

fix in #46

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants