From 0693348469f6f9b450b9d6b8a94bc01f2ed04f7e Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Sat, 5 Jul 2014 20:23:15 +0200 Subject: [PATCH] Adds Sentry middleware --- sentry/recovery.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 sentry/recovery.go diff --git a/sentry/recovery.go b/sentry/recovery.go new file mode 100644 index 0000000..60cb67f --- /dev/null +++ b/sentry/recovery.go @@ -0,0 +1,34 @@ +package sentry + +import ( + "errors" + "fmt" + "github.com/getsentry/raven-go" + "github.com/gin-gonic/gin" + "net/http" + "runtime/debug" +) + +func Recovery(client *raven.Client, onlyCrashes bool) gin.HandlerFunc { + return func(c *gin.Context) { + defer func() { + flags := map[string]string{ + "endpoint": c.Req.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, flags) + c.Writer.WriteHeader(http.StatusInternalServerError) + } + if !onlyCrashes { + for _, item := range c.Errors { + packet := raven.NewPacket(item.Message, &raven.Message{item.Message, []interface{}{item.Meta}}) + client.Capture(packet, flags) + } + } + }() + c.Next() + } +}