-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathrecovery.go
More file actions
34 lines (32 loc) · 855 Bytes
/
recovery.go
File metadata and controls
34 lines (32 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package recovery
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()
}
}