forked from gin-gonic/gin-sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recovery.go
43 lines (39 loc) · 949 Bytes
/
recovery.go
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
35
36
37
38
39
40
41
42
43
package recovery
import (
"errors"
"fmt"
"net/http"
"runtime/debug"
"github.com/gin-gonic/gin"
raven "github.com/qiscus/raven-go"
)
func Recovery(dsn string, onlyCrashes bool) gin.HandlerFunc {
err := raven.SetDSN(dsn)
if err != nil {
panic(err)
}
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.Capture(packet, flags)
c.Writer.WriteHeader(http.StatusInternalServerError)
}
if !onlyCrashes {
for _, item := range c.Errors {
packet := raven.NewPacket(item.Error(), &raven.Message{
Message: item.Error(),
Params: []interface{}{item.Meta}},
)
raven.Capture(packet, flags)
}
}
}()
c.Next()
}
}