-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecover.go
More file actions
63 lines (58 loc) · 1.31 KB
/
recover.go
File metadata and controls
63 lines (58 loc) · 1.31 KB
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
package peony
import (
"fmt"
"regexp"
"runtime/debug"
"strconv"
"strings"
)
//if app panic, the filter will handle the error.
func RecoverFilter(c *Controller, filter []Filter) {
defer func() {
if err := recover(); err != nil {
stack := debug.Stack()
ERRORWriter.Write(stack)
e := NewErrorFromPanic(c.Server.App, string(stack))
if e != nil {
description := "unknown error"
if err != nil {
description = fmt.Sprint(err)
}
e.Description = description
RenderError(e).Apply(c)
} else {
c.Resp.Write(stack)
}
//if error happend don't use render
c.render = nil
}
}()
filter[0](c, filter[1:])
}
var PANICRegexp = regexp.MustCompile(`^([^:#]+):(\d+)(:\d+)? (.*)$`)
func NewErrorFromPanic(app *App, stack string) *Error {
idx := strings.Index(stack, app.BasePath)
if idx == -1 {
return nil
}
end := strings.Index(stack[idx:], "\n")
if end == -1 {
return nil
}
errorLine := stack[idx : idx+end]
matched := PANICRegexp.FindStringSubmatch(errorLine)
if matched == nil {
return nil
}
filepath := matched[1]
lineNO := matched[2]
line, _ := strconv.Atoi(lineNO)
e := &Error{
Title: "Panic",
Path: filepath[len(app.BasePath):],
FileName: filepath[len(app.BasePath):],
Line: line,
SourceLines: MustReadLines(filepath),
}
return e
}