-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecover.go
More file actions
41 lines (36 loc) · 1000 Bytes
/
recover.go
File metadata and controls
41 lines (36 loc) · 1000 Bytes
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 cat
import (
"fmt"
"log"
"net/http"
"runtime"
"strings"
)
// print stack trace for debug
func trace(message string) string {
var pcs [32]uintptr
n := runtime.Callers(3, pcs[:]) // skip first 3 caller
var str strings.Builder
str.WriteString(message + "\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() HandlerFunc {
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, message)
}
}()
// defer recover 机制只能针对于当前函数以及直接调用的函数可能参数的 panic
// 在 Recovery 里面的 c.Next() 会执行后面最近的 handler
// 如果没有 c.Next(),则 handler 不是 Recovery 直接调用的函数,无法 recover
c.Next()
}
}