Skip to content

Commit a3bb829

Browse files
committed
feat: add fallback error template, add test
1 parent b9c31a8 commit a3bb829

File tree

9 files changed

+89
-7
lines changed

9 files changed

+89
-7
lines changed

errgoengine.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ func (e *ErrgoEngine) Analyze(workingPath, msg string) (*CompiledErrorTemplate,
9191
Document: doc,
9292
Nearest: WrapNode(doc, nearest),
9393
}
94+
} else if template == FallbackErrorTemplate {
95+
contextData.MainError = &MainError{
96+
ErrorNode: nil,
97+
Document: nil,
98+
Nearest: SyntaxNode{},
99+
}
94100
} else {
95101
// return error template for bugbuddy to handle
96102
// incomplete error messages
@@ -106,13 +112,18 @@ func (e *ErrgoEngine) Analyze(workingPath, msg string) (*CompiledErrorTemplate,
106112

107113
func (e *ErrgoEngine) Translate(template *CompiledErrorTemplate, contextData *ContextData) (mainExp string, fullExp string) {
108114
expGen := &ExplainGenerator{errorName: template.Name}
109-
fixGen := &BugFixGenerator{
110-
Document: contextData.MainError.Document,
115+
fixGen := &BugFixGenerator{}
116+
if contextData.MainError != nil {
117+
fixGen.Document = contextData.MainError.Document
111118
}
112119

113120
// execute error generator function
114121
template.OnGenExplainFn(contextData, expGen)
115-
template.OnGenBugFixFn(contextData, fixGen)
122+
123+
// execute bug fix generator function
124+
if template.OnGenBugFixFn != nil {
125+
template.OnGenBugFixFn(contextData, fixGen)
126+
}
116127

117128
output := e.OutputGen.Generate(contextData, expGen, fixGen)
118129
defer e.OutputGen.Reset()

error_template.go

+34-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func (tmp *CompiledErrorTemplate) StackTraceRegex() *regexp.Regexp {
3939
}
4040

4141
func (tmp *CompiledErrorTemplate) ExtractVariables(msg string) map[string]string {
42+
if tmp.Pattern == nil {
43+
return map[string]string{}
44+
}
4245
variables := map[string]string{}
4346
groupNames := tmp.Pattern.SubexpNames()
4447
for _, submatches := range tmp.Pattern.FindAllStringSubmatch(msg, -1) {
@@ -55,10 +58,13 @@ func (tmp *CompiledErrorTemplate) ExtractVariables(msg string) map[string]string
5558

5659
func (tmp *CompiledErrorTemplate) ExtractStackTrace(cd *ContextData) TraceStack {
5760
traceStack := TraceStack{}
58-
workingPath := cd.WorkingPath
61+
stackTraceRegex := tmp.StackTraceRegex()
62+
if stackTraceRegex == nil {
63+
return traceStack
64+
}
5965

66+
workingPath := cd.WorkingPath
6067
rawStackTraceItem := cd.Variables["stacktrace"]
61-
stackTraceRegex := tmp.StackTraceRegex()
6268
symbolGroupIdx := stackTraceRegex.SubexpIndex("symbol")
6369
pathGroupIdx := stackTraceRegex.SubexpIndex("path")
6470
posGroupIdx := stackTraceRegex.SubexpIndex("position")
@@ -94,6 +100,9 @@ func (tmp *CompiledErrorTemplate) ExtractStackTrace(cd *ContextData) TraceStack
94100
}
95101

96102
func (tmp *CompiledErrorTemplate) Match(str string) bool {
103+
if tmp == FallbackErrorTemplate {
104+
return true
105+
}
97106
return tmp.Pattern.MatchString(str)
98107
}
99108

@@ -187,5 +196,28 @@ func (tmps ErrorTemplates) Find(language, name string) *CompiledErrorTemplate {
187196
}
188197

189198
func TemplateKey(language, name string) string {
199+
if len(language) == 0 {
200+
return name
201+
}
190202
return fmt.Sprintf("%s.%s", language, name)
191203
}
204+
205+
var FallbackErrorTemplate = &CompiledErrorTemplate{
206+
ErrorTemplate: ErrorTemplate{
207+
Name: "UnknownError",
208+
Pattern: `.*`,
209+
OnGenExplainFn: func(cd *ContextData, gen *ExplainGenerator) {
210+
gen.Add("There are no available error templates for this error.\n")
211+
gen.Add("```\n")
212+
gen.Add(cd.Variables["message"])
213+
gen.Add("\n```")
214+
},
215+
},
216+
Language: &Language{
217+
AnalyzerFactory: func(cd *ContextData) LanguageAnalyzer {
218+
return nil
219+
},
220+
},
221+
Pattern: nil,
222+
StackTracePattern: nil,
223+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package error_templates_test
2+
3+
import (
4+
"testing"
5+
6+
lib "github.com/nedpals/errgoengine"
7+
testutils "github.com/nedpals/errgoengine/error_templates/test_utils"
8+
)
9+
10+
func TestFallbackErrorTemplate(t *testing.T) {
11+
testutils.SetupTest(t, testutils.SetupTestConfig{
12+
DirName: "fallback",
13+
TemplateLoader: func(et *lib.ErrorTemplates) {
14+
(*et)[lib.TemplateKey("", lib.FallbackErrorTemplate.Name)] = lib.FallbackErrorTemplate
15+
},
16+
}).Execute(t)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi im a random program!
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
template: "UnknownError"
2+
---
3+
oh no i'm an error! :<<<
4+
5+
===
6+
template: "UnknownError"
7+
---
8+
aaa

error_templates/test_utils/test_file_parser.go

+10
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,20 @@ func (p *Parser) ParseInputExpected(filename string, input string) (*TestOutput,
236236
return nil, nil, err
237237
}
238238

239+
if len(inputOut.Language) != 0 && len(inputOut.Template) == 0 {
240+
inputOut.Template = inputOut.Language
241+
inputOut.Language = ""
242+
}
243+
239244
expOut, err := p.Parse(strings.NewReader(rawOutputs[1]))
240245
if err != nil {
241246
return nil, nil, err
242247
}
243248

249+
if len(expOut.Language) != 0 && len(expOut.Template) == 0 {
250+
expOut.Template = expOut.Language
251+
expOut.Language = ""
252+
}
253+
244254
return inputOut, expOut, nil
245255
}

error_templates/test_utils/test_utils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func SetupTest(tb testing.TB, cfg SetupTestConfig) TestCases {
111111
return e
112112
}
113113

114-
if expTemp.Language.MatchPath(path) {
114+
if (expTemp == lib.FallbackErrorTemplate && !d.IsDir()) || expTemp.Language.MatchPath(path) {
115115
fileContent, err := os.ReadFile(path)
116116
if err != nil {
117117
return err

output_gen.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (gen *OutputGenerator) Generate(cd *ContextData, explain *ExplainGenerator,
8282
gen.generateFromExp(1, explain)
8383
doc := cd.MainError.Document
8484

85-
if gen.IsTesting && !cd.MainError.Nearest.IsNull() {
85+
if doc != nil && gen.IsTesting && !cd.MainError.Nearest.IsNull() {
8686
startLineNr := cd.MainError.Nearest.StartPosition().Line
8787
startLines := doc.LinesAt(max(startLineNr-1, 0), startLineNr)
8888
endLines := doc.LinesAt(min(startLineNr+1, doc.TotalLines()), min(startLineNr+2, doc.TotalLines()))

trace_stack.go

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ func (st *TraceStack) Add(symbolName string, loc Location) {
1212
}
1313

1414
func (st TraceStack) Top() StackTraceEntry {
15+
if len(st) == 0 {
16+
return StackTraceEntry{}
17+
}
1518
return st[len(st)-1]
1619
}
1720

0 commit comments

Comments
 (0)