-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
230 lines (205 loc) · 5.38 KB
/
middleware.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package utils
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"runtime"
"runtime/debug"
"strings"
"time"
log "github.com/Sirupsen/logrus"
"github.com/pkg/errors"
"github.com/stvp/rollbar"
"gopkg.in/gin-gonic/gin.v1"
"gopkg.in/go-playground/validator.v8"
"gopkg.in/olivere/elastic.v5"
)
// Set MDB, ES & LOGGER clients in context
func DataStoresMiddleware(mbdDB *sql.DB, esc *elastic.Client, logger interface{}) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("MDB_DB", mbdDB)
c.Set("ES_CLIENT", esc)
c.Set("LOGGER", logger)
c.Next()
}
}
func LoggerMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.RequestURI() // some evil middleware modify this values
c.Next()
log.WithFields(log.Fields{
"status": c.Writer.Status(),
"method": c.Request.Method,
"path": path,
"latency": time.Now().Sub(start),
"ip": c.ClientIP(),
"user-agent": c.Request.UserAgent(),
}).Info()
}
}
// Recover with error
func RecoveryMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if rval := recover(); rval != nil {
debug.PrintStack()
err, ok := rval.(error)
if !ok {
err = errors.Errorf("panic: %s", rval)
}
c.AbortWithError(http.StatusInternalServerError, err).SetType(gin.ErrorTypePrivate)
}
}()
c.Next()
}
}
func ValidationErrorMessage(e *validator.FieldError) string {
switch e.Tag {
case "required":
return "required"
case "max":
return fmt.Sprintf("cannot be longer than %s", e.Param)
case "min":
return fmt.Sprintf("must be longer than %s", e.Param)
case "len":
return fmt.Sprintf("must be %s characters long", e.Param)
case "email":
return "invalid email format"
case "hexadecimal":
return "invalid hexadecimal value"
default:
return "invalid value"
}
}
func BindErrorMessage(err error) string {
switch err.(type) {
case *json.SyntaxError:
e := err.(*json.SyntaxError)
return fmt.Sprintf("json: %s [offset: %d]", e.Error(), e.Offset)
case *json.UnmarshalTypeError:
e := err.(*json.UnmarshalTypeError)
return fmt.Sprintf("json: expecting %s got %s [offset: %d]", e.Type.String(), e.Value, e.Offset)
default:
return err.Error()
}
}
type stackTracer interface {
StackTrace() errors.StackTrace
}
func errorsToRollbarStack(st stackTracer) rollbar.Stack {
t := st.StackTrace()
rs := make(rollbar.Stack, len(t))
for i, f := range t {
// Program counter as it's computed internally in errors.Frame
pc := uintptr(f) - 1
fn := runtime.FuncForPC(pc)
if fn == nil {
rs[i] = rollbar.Frame{
Filename: "unknown",
Method: "?",
Line: 0,
}
continue
}
// symtab info
file, line := fn.FileLine(pc)
name := fn.Name()
// trim compile time GOPATH from file name
fileWImportPath := trimGOPATH(name, file)
// Strip only method name from FQN
idx := strings.LastIndex(name, "/")
name = name[idx+1:]
idx = strings.Index(name, ".")
name = name[idx+1:]
rs[i] = rollbar.Frame{
Filename: fileWImportPath,
Method: name,
Line: line,
}
}
return rs
}
// Taken AS IS from errors pkg since it's not exported there.
// Check out the source code with good comments on https://github.com/pkg/errors/blob/master/stack.go
func trimGOPATH(name, file string) string {
const sep = "/"
goal := strings.Count(name, sep) + 2
i := len(file)
for n := 0; n < goal; n++ {
i = strings.LastIndex(file[:i], sep)
if i == -1 {
i = -len(sep)
break
}
}
file = file[i+len(sep):]
return file
}
// Handle all errors
func ErrorHandlingMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Next()
if len(c.Errors) > 0 {
for _, e := range c.Errors {
switch e.Type {
case gin.ErrorTypePublic:
if e.Err != nil {
log.Warnf("Public error: %s", e.Error())
c.JSON(c.Writer.Status(), gin.H{"status": "error", "error": e.Error()})
}
case gin.ErrorTypeBind:
// Keep the preset response status
status := http.StatusBadRequest
if c.Writer.Status() != http.StatusOK {
status = c.Writer.Status()
}
switch e.Err.(type) {
case validator.ValidationErrors:
errs := e.Err.(validator.ValidationErrors)
errMap := make(map[string]string)
for field, err := range errs {
msg := ValidationErrorMessage(err)
log.WithFields(log.Fields{
"field": field,
"error": msg,
}).Warn("Validation error")
errMap[err.Field] = msg
}
c.JSON(status, gin.H{"status": "error", "errors": errMap})
default:
log.WithFields(log.Fields{
"error": e.Err.Error(),
}).Warn("Bind error")
c.JSON(status, gin.H{
"status": "error",
"error": BindErrorMessage(e.Err),
})
}
default:
// Log all other errors
log.Error(e.Err)
st, ok := e.Err.(stackTracer)
if ok {
fmt.Printf("%s: %+v\n", st, st.StackTrace())
}
// Log to rollbar if we have a token setup
if len(rollbar.Token) != 0 {
if ok {
rollbar.RequestErrorWithStack(rollbar.ERR, c.Request, e.Err,
errorsToRollbarStack(st))
} else {
rollbar.RequestError(rollbar.ERR, c.Request, e.Err)
}
}
}
}
// If there was no public or bind error, display default 500 message
if !c.Writer.Written() {
c.JSON(http.StatusInternalServerError,
gin.H{"status": "error", "error": "Internal Server Error"})
}
}
}
}