-
Notifications
You must be signed in to change notification settings - Fork 1
/
recovery.go
68 lines (62 loc) · 1.74 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
package ginmiddleware
import (
"errors"
"io"
"net"
"net/http"
"net/http/httputil"
"os"
"runtime/debug"
"strings"
"github.com/gin-gonic/gin"
)
const (
DebugStack string = "DebugStack"
)
func Recovery(writer io.Writer) gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
logger := CtxLogger(ctx).Output(writer)
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 {
var se *os.SyscallError
if errors.As(ne, &se) {
if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
brokenPipe = true
}
}
}
stack := debug.Stack()
if e, ok := err.(error); ok {
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 {
logger.Err(e).Bytes(DebugStack, stack).Strs("panicHeader", headers).Msg("net op error panic")
} else {
logger.Err(e).Bytes(DebugStack, stack).Msg("panic error")
}
} else {
logger.Error().Bytes(DebugStack, stack).Msg("panic")
}
if brokenPipe {
// If the connection is dead, we can't write a status to it.
c.Error(err.(error)) // nolint: errcheck
c.Abort()
} else {
c.AbortWithStatusJSON(http.StatusInternalServerError, map[string]string{"message": "server panic"})
}
}
}()
c.Next()
}
}