-
Notifications
You must be signed in to change notification settings - Fork 5
/
pipe_recovery.go
43 lines (37 loc) · 966 Bytes
/
pipe_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
package xingyun
import (
"fmt"
"net/http"
"runtime"
"strings"
)
func DefaultPanicHandler(ctx *Context) {
w := ctx.ResponseWriter
r := ctx.Request
w.WriteHeader(http.StatusInternalServerError)
ctx.Logger.Errorf("%s %s: %s\n%s", r.Method, r.RequestURI, ctx.PanicError, ctx.StackMessage)
}
func (s *Server) GetRecoverPipeHandler() PipeHandler {
return PipeHandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
s.logger.Tracef("enter recover handler")
defer func() {
if err := recover(); err != nil {
ctx := GetContext(r)
ctx.IsPanic = true
ctx.PanicError = err
var stacks []string
for i := 1; ; i += 1 {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
stacks = append(stacks, fmt.Sprintf("\t%s:%d", file, line))
}
ctx.StackMessage = strings.Join(stacks, "\n")
s.PanicHandler(ctx)
}
s.logger.Tracef("exit recover handler")
}()
next(w, r)
})
}