Skip to content

Commit 05375fa

Browse files
committed
feat: extract code from errgoengine.go, add more tests
1 parent b87fb9e commit 05375fa

File tree

6 files changed

+313
-58
lines changed

6 files changed

+313
-58
lines changed

context.go

+14
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,17 @@ func (data *ContextData) AddVariable(name string, value string) {
5353

5454
data.Variables[name] = value
5555
}
56+
57+
func (data *ContextData) AddVariables(vars map[string]string) {
58+
if len(vars) == 0 {
59+
return
60+
}
61+
62+
if data.Variables == nil {
63+
data.Variables = make(map[string]string)
64+
}
65+
66+
for k, v := range vars {
67+
data.Variables[k] = v
68+
}
69+
}

errgoengine.go

+3-45
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"io/fs"
7-
"path/filepath"
87

98
sitter "github.com/smacker/go-tree-sitter"
109
)
@@ -58,52 +57,11 @@ func (e *ErrgoEngine) Analyze(workingPath, msg string) (*CompiledErrorTemplate,
5857
contextData.Analyzer = template.Language.AnalyzerFactory(contextData)
5958
contextData.AddVariable("message", msg)
6059

61-
groupNames := template.Pattern.SubexpNames()
62-
for _, submatches := range template.Pattern.FindAllStringSubmatch(msg, -1) {
63-
for idx, matchedContent := range submatches {
64-
if len(groupNames[idx]) == 0 {
65-
continue
66-
}
67-
68-
contextData.AddVariable(groupNames[idx], matchedContent)
69-
}
70-
}
60+
// extract variables from the error message
61+
contextData.AddVariables(template.ExtractVariables(msg))
7162

7263
// extract stack trace
73-
rawStackTraceItem := contextData.Variables["stacktrace"]
74-
symbolGroupIdx := template.StackTraceRegex().SubexpIndex("symbol")
75-
pathGroupIdx := template.StackTraceRegex().SubexpIndex("path")
76-
posGroupIdx := template.StackTraceRegex().SubexpIndex("position")
77-
stackTraceMatches := template.StackTraceRegex().FindAllStringSubmatch(rawStackTraceItem, -1)
78-
79-
for _, submatches := range stackTraceMatches {
80-
if len(submatches) == 0 {
81-
continue
82-
}
83-
84-
rawSymbolName := ""
85-
if symbolGroupIdx != -1 {
86-
rawSymbolName = submatches[symbolGroupIdx]
87-
}
88-
rawPath := submatches[pathGroupIdx]
89-
rawPos := submatches[posGroupIdx]
90-
91-
// convert relative paths to absolute for parsing
92-
if len(workingPath) != 0 && !filepath.IsAbs(rawPath) {
93-
rawPath = filepath.Clean(filepath.Join(workingPath, rawPath))
94-
}
95-
96-
stLoc := template.Language.LocationConverter(LocationConverterContext{
97-
Path: rawPath,
98-
Pos: rawPos,
99-
ContextData: contextData,
100-
})
101-
if contextData.TraceStack == nil {
102-
contextData.TraceStack = TraceStack{}
103-
}
104-
105-
contextData.TraceStack.Add(rawSymbolName, stLoc)
106-
}
64+
contextData.TraceStack = template.ExtractStackTrace(contextData)
10765

10866
// open contents of the extracted stack file locations
10967
parser := sitter.NewParser()

error_template.go

+56
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package errgoengine
22

33
import (
44
"fmt"
5+
"path/filepath"
56
"regexp"
67
"strings"
78
)
@@ -37,6 +38,61 @@ func (tmp *CompiledErrorTemplate) StackTraceRegex() *regexp.Regexp {
3738
return tmp.Language.stackTraceRegex
3839
}
3940

41+
func (tmp *CompiledErrorTemplate) ExtractVariables(msg string) map[string]string {
42+
variables := map[string]string{}
43+
groupNames := tmp.Pattern.SubexpNames()
44+
for _, submatches := range tmp.Pattern.FindAllStringSubmatch(msg, -1) {
45+
for idx, matchedContent := range submatches {
46+
if len(groupNames[idx]) == 0 {
47+
continue
48+
}
49+
50+
variables[groupNames[idx]] = matchedContent
51+
}
52+
}
53+
return variables
54+
}
55+
56+
func (tmp *CompiledErrorTemplate) ExtractStackTrace(cd *ContextData) TraceStack {
57+
traceStack := TraceStack{}
58+
workingPath := cd.WorkingPath
59+
60+
rawStackTraceItem := cd.Variables["stacktrace"]
61+
stackTraceRegex := tmp.StackTraceRegex()
62+
symbolGroupIdx := stackTraceRegex.SubexpIndex("symbol")
63+
pathGroupIdx := stackTraceRegex.SubexpIndex("path")
64+
posGroupIdx := stackTraceRegex.SubexpIndex("position")
65+
stackTraceMatches := stackTraceRegex.FindAllStringSubmatch(rawStackTraceItem, -1)
66+
67+
for _, submatches := range stackTraceMatches {
68+
if len(submatches) == 0 {
69+
continue
70+
}
71+
72+
rawSymbolName := ""
73+
if symbolGroupIdx != -1 {
74+
rawSymbolName = submatches[symbolGroupIdx]
75+
}
76+
rawPath := submatches[pathGroupIdx]
77+
rawPos := submatches[posGroupIdx]
78+
79+
// convert relative paths to absolute for parsing
80+
if len(workingPath) != 0 && !filepath.IsAbs(rawPath) {
81+
rawPath = filepath.Clean(filepath.Join(workingPath, rawPath))
82+
}
83+
84+
stLoc := tmp.Language.LocationConverter(LocationConverterContext{
85+
Path: rawPath,
86+
Pos: rawPos,
87+
ContextData: cd,
88+
})
89+
90+
traceStack.Add(rawSymbolName, stLoc)
91+
}
92+
93+
return traceStack
94+
}
95+
4096
type ErrorTemplates map[string]*CompiledErrorTemplate
4197

4298
const defaultStackTraceRegex = `(?P<stacktrace>(?:.|\s)*)`

0 commit comments

Comments
 (0)