-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrecovery.go
66 lines (61 loc) · 1.77 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
package kuu
import (
"net"
"net/http"
"net/http/httputil"
"os"
"strings"
"time"
"github.com/gin-gonic/gin"
)
// Recovery defined kuu.Engine recovery from panic
// Rewrite `Recovery` if you need
// Tag: 在rest不处理error,除非业务需求(如事物),直接抛出来
var Recovery gin.HandlerFunc
func recovery() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if err := recover(); err != nil {
// Check for a broken connection, as it is not really a
// condition that warrants a panic stack trace.
var brokenPipe bool
if ne, ok := err.(*net.OpError); ok {
if se, ok := ne.Err.(*os.SyscallError); ok {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
stack := stack(3)
httpRequest, _ := httputil.DumpRequest(c.Request, false)
headers := strings.Split(string(httpRequest), "\r\n")
for idx, header := range headers {
current := strings.Split(header, ":")
if current[0] == "Authorization" {
headers[idx] = current[0] + ": *"
}
}
if brokenPipe {
ERROR("%s\n%s", err, string(httpRequest))
} else if gin.IsDebugging() {
ERROR("kuu %s panic recovered:\n%s\n%s\n%s",
timeFormat(time.Now()), strings.Join(headers, "\r\n"), err, stack)
} else {
ERROR("kuu %s panic recovered:\n%s\n%s",
timeFormat(time.Now()), err, stack)
}
// If the connection is dead, we can't write a status to it.
if brokenPipe {
c.Error(err.(error)) // nolint: errcheck
c.Abort()
} else {
c.String(http.StatusOK, "sorry, sys exception, please try again later")
}
}
}()
c.Next()
}
}
func init() {
Recovery = recovery()
}