forked from osuthailand/hanayo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recovery.go
104 lines (91 loc) · 2.01 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
raven "github.com/getsentry/raven-go"
"github.com/gin-gonic/gin"
)
// Recovery is a better sentry logger.
func Recovery(client *raven.Client, onlyCrashes bool) gin.HandlerFunc {
return func(c *gin.Context) {
var requestBody []byte
defer func() {
st := raven.NewStacktrace(0, 3, []string{"git.github.com/osuthailand"})
tokenRaw, ex := c.Get("token")
var token string
if ex {
token = tokenRaw.(string)
}
ravenHTTP := raven.NewHttp(c.Request)
if len(requestBody) != 0 {
ravenHTTP.Data = string(requestBody)
}
ravenUser := &raven.User{
Username: token,
IP: c.Request.RemoteAddr,
}
flags := map[string]string{
"endpoint": c.Request.RequestURI,
"token": token,
}
if rval := recover(); rval != nil {
var err error
switch rval := rval.(type) {
case string:
err = errors.New(rval)
case error:
err = rval
default:
err = fmt.Errorf("%v - %#v", rval, rval)
}
fmt.Println(err)
client.CaptureError(
err,
flags,
st,
ravenHTTP,
ravenUser,
)
c.AbortWithStatus(http.StatusInternalServerError)
}
if !onlyCrashes {
for _, item := range c.Errors {
var err = error(item)
if item.Type == gin.ErrorTypePrivate {
err = item.Err
}
fmt.Println(err)
client.CaptureError(
err,
flags,
ravenHTTP,
ravenUser,
)
}
}
}()
if c.Request.Method == "POST" && c.Request.URL.Path != "/tokens" &&
c.Request.URL.Path != "/tokens/new" {
var err error
requestBody, err = ioutil.ReadAll(c.Request.Body)
if err != nil {
c.Error(err)
}
c.Request.Body = fakeBody{
r: bytes.NewReader(requestBody),
orig: c.Request.Body,
}
}
c.Next()
}
}
type fakeBody struct {
r io.Reader
orig io.ReadCloser
}
func (f fakeBody) Read(p []byte) (int, error) { return f.r.Read(p) }
func (f fakeBody) Close() error { return f.orig.Close() }