-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger.go
42 lines (34 loc) · 1.08 KB
/
debugger.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
//This software is licensed under the MIT License.
//You can get more info in license file.
package smile
import (
"fmt"
"runtime/debug"
)
//Debugger 定义外部panic处理函数,可用于覆盖默认值
type Debugger func(*Context, error)
// 储存一个处理panic的函数
var debugger Debugger
func init() {
//使用默认的recover处理函数
debugger = defaultDebugger
}
// debug钩子 handleFunc 返回error时被调用
// 调用注册的error处理函数
func doDebug(e error, c *Context) {
debugger(c, e)
}
//默认debug函数
func defaultDebugger(cb *Context, e error) {
stack := debug.Stack()
fmt.Printf("[debug_log] error: %s\n", e.Error())
fmt.Printf("[debug_log] stacks: %s\n", string(stack))
cb.Header().Add("Content-Type", "text/html;charset=utf-8;")
cb.WriteHeader(cb.Status())
str := fmt.Sprintf("{\"path\":\"%s\",\"status\":\"500\",\"message\":\"Internal Server Error \r\n %s\"}", cb.Request.URL.Path,e.Error())
cb.WriteString(str)
}
//SetDebugger 由外部注入一个处理error的函数 会替换默认函数
func SetDebugger(fnc Debugger) {
debugger = fnc
}