-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrecovey.go
46 lines (38 loc) · 905 Bytes
/
recovey.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
44
45
46
package rollbar
import (
"errors"
"fmt"
"net/http"
"runtime"
"runtime/debug"
"github.com/gin-gonic/gin"
"github.com/rollbar/rollbar-go"
)
// Recovery middleware for rollbar error monitoring
func Recovery(onlyCrashes bool) gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if rval := recover(); rval != nil {
debug.PrintStack()
rollbar.Critical(errors.New(fmt.Sprint(rval)), getCallers(3), map[string]string{
"endpoint": c.Request.RequestURI,
})
c.AbortWithStatus(http.StatusInternalServerError)
}
if !onlyCrashes {
for _, item := range c.Errors {
rollbar.Error(item.Err, map[string]string{
"meta": fmt.Sprint(item.Meta),
"endpoint": c.Request.RequestURI,
})
}
}
}()
c.Next()
}
}
func getCallers(skip int) (pc []uintptr) {
pc = make([]uintptr, 1000)
i := runtime.Callers(skip+1, pc)
return pc[0:i]
}