/
recovery.go
72 lines (66 loc) · 2.12 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
// Package middleware
// @program: arco-design-pro-gin
// @author: [lliuhuan](https://github.com/lliuhuan)
// @create: 2021-12-10 14:20
package middleware
import (
"errors"
"net"
"net/http"
"net/http/httputil"
"os"
"runtime/debug"
"strings"
"github.com/lliuhuan/arco-design-pro-gin/model/common/response"
"github.com/gin-gonic/gin"
"github.com/lliuhuan/arco-design-pro-gin/global"
"go.uber.org/zap"
)
// GinRecovery recover掉项目可能出现的panic
func GinRecovery(stack bool) 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
}
}
}
httpRequest, _ := httputil.DumpRequest(c.Request, false)
if brokenPipe {
global.AdpLog.Error(c.Request.URL.Path,
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
// If the connection is dead, we can't write a status to it.
//c.Error(err.(error)) // nolint: errcheck
_ = c.Error(errors.New("系统内部错误")) // nolint: errcheck
response.ResultAll(http.StatusInternalServerError, gin.H{}, "系统内部错误-1", c)
c.Abort()
return
}
if stack {
global.AdpLog.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
zap.String("stack", string(debug.Stack())),
)
response.ResultAll(http.StatusInternalServerError, gin.H{}, "系统内部错误-2", c)
} else {
global.AdpLog.Error("[Recovery from panic]",
zap.Any("error", err),
zap.String("request", string(httpRequest)),
)
response.ResultAll(http.StatusInternalServerError, gin.H{}, "系统内部错误-3", c)
}
c.AbortWithStatus(http.StatusInternalServerError)
}
}()
c.Next()
}
}