-
Notifications
You must be signed in to change notification settings - Fork 2
/
jerror.go
170 lines (156 loc) · 3.38 KB
/
jerror.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package mcla
import (
"io"
"regexp"
"strings"
)
var (
javaErrorMatcher = regexp.MustCompile(`^\s*(?:\s*Exception in thread "[^"]+"\s+)?([\w\d$_]+(?:\.[\w\d$_]+)+):\s+(.*)$`)
stackInfoMatcher = regexp.MustCompile(`^\s*at\s+([\w\d$_]+(?:\.[\w\d$_]+)+)\.([\w\d$_<>]+)`)
)
type (
JavaError struct {
Class string `json:"class"`
Message string `json:"message"`
Stacktrace Stacktrace `json:"stacktrace"`
CausedBy *JavaError `json:"causedBy"`
// extra infos
LineNo int `json:"lineNo"` // which line did the error start
}
StackInfo struct {
Raw string `json:"raw"`
Class string `json:"class"`
Method string `json:"method"`
}
// Stacktrace:
Stacktrace []StackInfo
)
func parseStackInfoFrom(line string) (s StackInfo, ok bool) {
res := stackInfoMatcher.FindStringSubmatch(line)
if res == nil {
return
}
s.Raw = line
s.Class = res[1]
s.Method = res[2]
ok = true
return
}
func parseStacktrace(sc *lineScanner) (st Stacktrace) {
if !sc.Scan() {
return
}
return parseStacktrace0(sc)
}
func parseStacktrace0(sc *lineScanner) (st Stacktrace) {
var (
info StackInfo
ok bool
)
for {
line := sc.Text()
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "... ") && strings.HasSuffix(line, " more") {
sc.Scan() // move to the next line
return
}
if info, ok = parseStackInfoFrom(line); !ok {
return
}
st = append(st, info)
if !sc.Scan() {
return
}
}
}
func parseJavaError(sc *lineScanner) (je *JavaError) {
if !sc.Scan() {
return
}
return parseJavaError0(sc.Text(), sc)
}
func parseJavaError0(line string, sc *lineScanner) (je *JavaError) {
je = new(JavaError)
i := strings.IndexByte(line, ':')
if i == -1 {
je.Class = line
} else {
je.Class, je.Message = line[:i], strings.TrimSpace(line[i+1:])
}
je.LineNo = sc.Count()
je.Stacktrace = parseStacktrace(sc)
if line, ok := strings.CutPrefix(strings.TrimSpace(sc.Text()), "Caused by: "); ok {
je.CausedBy = parseJavaError0(line, sc)
}
return
}
func scanJavaErrors(r io.Reader, cb func(*JavaError)) (err error) {
sc := newLineScanner(r)
if !sc.Scan() {
return sc.Err()
}
var (
line string
lineNo int
)
for {
line = sc.Text()
lineNo = sc.Count()
emsg := javaErrorMatcher.FindStringSubmatch(line)
if !sc.Scan() {
return sc.Err()
}
if emsg == nil {
continue
}
for {
l2 := sc.Text()
if stackInfoMatcher.MatchString(l2) {
break
}
if em := javaErrorMatcher.FindStringSubmatch(l2); em != nil {
line = l2
emsg = em
} else {
emsg[2] += "\n" + l2
}
if !sc.Scan() {
break
}
}
st := parseStacktrace0(sc)
if st != nil { // if stacktrace exists
je := &JavaError{
Class: emsg[1],
Message: emsg[2],
Stacktrace: st,
LineNo: lineNo,
}
if line, ok := strings.CutPrefix(strings.TrimSpace(sc.Text()), "Caused by: "); ok {
je.CausedBy = parseJavaError0(line, sc)
}
cb(je)
}
}
}
func ScanJavaErrors(r io.Reader) (res []*JavaError, err error) {
res = make([]*JavaError, 0, 3)
err = scanJavaErrors(r, func(je *JavaError) {
res = append(res, je)
})
return
}
func ScanJavaErrorsIntoChan(r io.Reader) (<-chan *JavaError, <-chan error) {
resCh := make(chan *JavaError, 3)
errCh := make(chan error, 0)
go func() {
defer close(resCh)
err := scanJavaErrors(r, func(je *JavaError) {
resCh <- je
})
if err != nil {
errCh <- err
}
}()
return resCh, errCh
}