forked from cccoven/mini-gin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecovery.go
41 lines (34 loc) · 843 Bytes
/
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
package mini_gin
import (
"fmt"
"log"
"net/http"
"runtime"
"strings"
)
// trace 打印追踪栈
func trace(msg string) string {
var pcs [32]uintptr
n := runtime.Callers(3, pcs[:])
var str strings.Builder
str.WriteString(msg + "\nTraceback: ")
for _, pc := range pcs[:n] {
fn := runtime.FuncForPC(pc)
file, line := fn.FileLine(pc)
str.WriteString(fmt.Sprintf("\n\t%s: %d", file, line))
}
return str.String()
}
func Recovery() HandleFunc {
return func(c *Context) {
defer func() {
if err := recover(); err != nil {
message := fmt.Sprintf("%s", err)
log.Printf("%s\n\n", trace(message))
c.Fail(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
}()
// 如果执行 Next() 的过程中发生了 panic,那么就会进入 defer 的 recover() 流程
c.Next()
}
}