hanayo/vendor/github.com/gin-gonic/contrib/sentry/recovery.go
2019-02-23 13:29:15 +00:00

45 lines
972 B
Go

package sentry
import (
"errors"
"fmt"
"net/http"
"runtime/debug"
"github.com/getsentry/raven-go"
"github.com/gin-gonic/gin"
)
func Recovery(client *raven.Client, onlyCrashes bool) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
flags := map[string]string{
"endpoint": c.Request.RequestURI,
}
if rval := recover(); rval != nil {
debug.PrintStack()
rvalStr := fmt.Sprint(rval)
packet := raven.NewPacket(rvalStr,
raven.NewException(errors.New(rvalStr), raven.NewStacktrace(2, 3, nil)),
raven.NewHttp(c.Request))
client.Capture(packet, flags)
c.AbortWithStatus(http.StatusInternalServerError)
}
if !onlyCrashes {
for _, item := range c.Errors {
packet := raven.NewPacket(item.Error(),
&raven.Message{
Message: item.Error(),
Params: []interface{}{item.Meta},
},
raven.NewHttp(c.Request))
client.Capture(packet, flags)
}
}
}()
c.Next()
}
}