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

43 lines
953 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)))
client.Capture(packet, map[string]string{
"endpoint": c.Request.RequestURI,
})
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},
})
client.Capture(packet, flags)
}
}
}()
c.Next()
}
}