Skip to content

Commit

Permalink
Adds Sentry middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
manucorporat committed Jul 5, 2014
0 parents commit 0693348
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions sentry/recovery.go
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 0693348

Please sign in to comment.