sentry-go icon indicating copy to clipboard operation
sentry-go copied to clipboard

panic is catched by gin.Recovery() in gin

Open xdkaka opened this issue 3 years ago • 1 comments

Summary

In Gin r.Use(gin.Recovery()) will catch panic sentry can't catch panic I try use param(Repanic: true),but it can't catch panic too

Steps To Reproduce


Expected Behavior

Screenshots

Sentry.io Event

Environment

SDK

  • sentry-go version:
  • Go version:
  • Using Go Modules? [yes/no]

Sentry

  • Using hosted Sentry in sentry.io? [yes/no]
  • Using your own Sentry installation? Version:
  • Anything particular to your environment that could be related to this issue?

Additional context

xdkaka avatar Jul 27 '21 14:07 xdkaka

Hi @xdkaka! Because Gin passes control to middleware in the same order as they are registered, you have to use the Sentry middleware before the Recovery middleware from Gin, see https://github.com/gin-gonic/gin#blank-gin-without-middleware-by-default.

Use

r := gin.New()

instead of

// Default With the Logger and Recovery middleware already attached
r := gin.Default()

Example:

func main() {
	// Creates a router without any middleware by default
	r := gin.New()

	r.Use(gin.Logger())

	// Sentry middleware before Recovery middleware.
	r.Use(sentrygin.New(sentrygin.Options{
		Repanic: true,
	}))

	// Recovery middleware recovers from any panics and writes a 500 if there was one.
	r.Use(gin.Recovery())
	
	// ...
}

I noticed that our example and docs are wrong in using gin.Default() with Repanic: true, I'll go ahead and fix that.

https://github.com/getsentry/sentry-go/blob/8a528f85d1952e6690c627caec2efe7f010a6964/example/gin/main.go#L29-L33

https://docs.sentry.io/platforms/go/guides/gin/

rhcarvalho avatar Jul 28 '21 11:07 rhcarvalho