-
Notifications
You must be signed in to change notification settings - Fork 136
/
compiled_template_error.go
182 lines (150 loc) · 5.04 KB
/
compiled_template_error.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
171
172
173
174
175
176
177
178
179
180
181
182
package template
import (
"fmt"
"strings"
"github.com/k14s/ytt/pkg/filepos"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
)
type CompiledTemplateMultiError struct {
errs []CompiledTemplateError
loader CompiledTemplateLoader
}
var _ error = CompiledTemplateMultiError{}
type CompiledTemplateError struct {
Positions []CompiledTemplateErrorPosition
Msg string
}
type CompiledTemplateErrorPosition struct {
Filename string
ContextName string
TemplateLine *TemplateLine
BeforeTemplateLine *TemplateLine
AfterTemplateLine *TemplateLine
}
func NewCompiledTemplateMultiError(err error, loader CompiledTemplateLoader) error {
e := CompiledTemplateMultiError{loader: loader}
switch typedErr := err.(type) {
case syntax.Error:
e.errs = append(e.errs, CompiledTemplateError{
Positions: []CompiledTemplateErrorPosition{e.buildPos(typedErr.Pos)},
Msg: typedErr.Msg,
})
case resolve.ErrorList:
for _, resolveErr := range typedErr {
e.errs = append(e.errs, CompiledTemplateError{
Positions: []CompiledTemplateErrorPosition{e.buildPos(resolveErr.Pos)},
Msg: resolveErr.Msg,
})
}
case *starlark.EvalError:
e.errs = append(e.errs, e.buildEvalErr(typedErr))
default:
e.errs = append(e.errs, CompiledTemplateError{Msg: err.Error()})
}
return e
}
func (e CompiledTemplateMultiError) Error() string {
result := []string{""}
for _, err := range e.errs {
var topicLine string
var otherLines []string
if !strings.Contains(err.Msg, "\n") {
topicLine = err.Msg
} else {
for i, line := range strings.Split(err.Msg, "\n") {
if i == 0 {
topicLine = line
} else {
otherLines = append(otherLines, line)
}
}
}
result = append(result, fmt.Sprintf("- %s", topicLine))
for _, pos := range err.Positions {
// TODO do better
if pos.TemplateLine == nil {
continue
}
linePad := " "
if len(pos.ContextName) > 0 {
result = append(result, linePad+"in "+pos.ContextName)
linePad += " "
}
if pos.TemplateLine.SourceLine != nil {
if pos.TemplateLine.SourceLine.Selection != nil {
result = append(result, fmt.Sprintf("%s%s%s",
linePad, e.posPrefixStr(pos.TemplateLine.SourceLine.Selection), pos.TemplateLine.SourceLine.Selection.Content))
} else {
result = append(result, fmt.Sprintf("%s%s%s",
linePad, e.posPrefixStr(pos.TemplateLine.SourceLine), pos.TemplateLine.SourceLine.Content))
}
} else {
if pos.BeforeTemplateLine != nil && pos.BeforeTemplateLine.SourceLine != nil {
result = append(result, fmt.Sprintf("%s%s%s",
linePad, e.posPrefixStr(pos.BeforeTemplateLine.SourceLine), pos.BeforeTemplateLine.SourceLine.Content))
}
result = append(result, fmt.Sprintf("%s%s:? | %s (generated)",
linePad, pos.Filename, pos.TemplateLine.Instruction.AsString()))
if pos.AfterTemplateLine != nil && pos.AfterTemplateLine.SourceLine != nil {
result = append(result, fmt.Sprintf("%s%s%s",
linePad, e.posPrefixStr(pos.AfterTemplateLine.SourceLine), pos.AfterTemplateLine.SourceLine.Content))
}
}
}
if len(otherLines) > 0 {
result = append(result, []string{"", fmt.Sprintf(" reason:")}...)
for _, line := range otherLines {
result = append(result, fmt.Sprintf(" %s", line))
}
}
}
return strings.Join(result, "\n")
}
func (e CompiledTemplateMultiError) posPrefixStr(srcLine *SourceLine) string {
// TODO show column information
return fmt.Sprintf("%s | ", srcLine.Position.AsCompactString())
}
func (e CompiledTemplateMultiError) buildEvalErr(err *starlark.EvalError) CompiledTemplateError {
// fmt.Printf("frame:\n%s\n", err.Backtrace())
result := CompiledTemplateError{Msg: err.Msg}
for i := len(err.CallStack) - 1; i >= 0; i-- {
pos := e.buildPos(err.CallStack[i].Pos)
pos.ContextName = err.CallStack[i].Name
result.Positions = append(result.Positions, pos)
}
return result
}
func (e CompiledTemplateMultiError) buildPos(pos syntax.Position) CompiledTemplateErrorPosition {
// TODO seems to be a bug in starlark where, for example,
// "function call2 takes exactly 1 positional argument (0 given)"
// error has 0 line number position (even though its 1 based)
if pos.Line == 0 {
return CompiledTemplateErrorPosition{}
}
ct, err := e.loader.FindCompiledTemplate(pos.Filename())
if err != nil {
panic(fmt.Errorf("Expected to find compiled template: %s", err))
}
line := ct.CodeAtLine(filepos.NewPosition(int(pos.Line)))
if line == nil {
panic(fmt.Errorf("Expected to find compiled template line %d", pos.Line))
}
return CompiledTemplateErrorPosition{
Filename: pos.Filename(),
TemplateLine: line,
BeforeTemplateLine: e.findClosestLine(ct, int(pos.Line), -1),
AfterTemplateLine: e.findClosestLine(ct, int(pos.Line), 1),
}
}
func (CompiledTemplateMultiError) findClosestLine(ct *CompiledTemplate, posLine int, lineInc int) *TemplateLine {
currPosLine := posLine
for {
currPosLine += lineInc
line := ct.CodeAtLine(filepos.NewPosition(currPosLine))
if line == nil || line.SourceLine != nil {
return line
}
}
}