Skip to content

Commit a5420e7

Browse files
committed
better error reporting details to stderr
1 parent 34744c9 commit a5420e7

File tree

1 file changed

+67
-1
lines changed

1 file changed

+67
-1
lines changed

chartsconfig/parser.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package chartsconfig
22

33
import (
4+
"bufio"
5+
"bytes"
46
"errors"
7+
"fmt"
58
"html/template"
69
"io/ioutil"
710
"os"
811
"path/filepath"
12+
"strconv"
913
"strings"
1014

1115
"github.com/Masterminds/semver"
@@ -53,7 +57,7 @@ func NewChartsConfiguration(input []byte, tpath string) (*ChartsConfiguration, e
5357
out := []byte(tpls["kubecrt/charts.yml"])
5458

5559
if err = yaml.Unmarshal(out, m); err != nil {
56-
return nil, err
60+
return nil, wrapError(out, err)
5761
}
5862

5963
for _, a := range m.ChartsMap {
@@ -164,3 +168,65 @@ func loadTemplates(b []byte, partialPath string) ([]*hchart.Template, error) {
164168

165169
return tpls, err
166170
}
171+
172+
func wrapError(b []byte, err error) error {
173+
var lines []string
174+
shortLines := []string{"\n"}
175+
176+
el, str := errorLine(err)
177+
178+
scanner := bufio.NewScanner(bytes.NewReader(b))
179+
l := 0
180+
for scanner.Scan() {
181+
l++
182+
}
183+
ll := len(strconv.Itoa(l))
184+
185+
i := 1
186+
scanner = bufio.NewScanner(bytes.NewReader(b))
187+
for scanner.Scan() {
188+
line := fmt.Sprintf("%*d: %s", ll, i, scanner.Text())
189+
190+
// if we know the error line, we create an extra summary of the context
191+
// surrounding the error itself, starting 3 lines before, ending 3 after.
192+
if el != 0 {
193+
if i == el {
194+
line = "\x1b[31;1m" + line + "\x1b[0m"
195+
}
196+
197+
if (i >= el-3) && (i <= el+3) {
198+
shortLines = append(shortLines, line)
199+
}
200+
}
201+
202+
lines = append(lines, line)
203+
i++
204+
}
205+
206+
lines = append(lines, shortLines...)
207+
lines = append(lines, "\n"+str)
208+
209+
return errors.New(strings.Join(lines, "\n"))
210+
}
211+
212+
func errorLine(err error) (int, string) {
213+
var i int
214+
var p []string
215+
str := err.Error()
216+
217+
println(str)
218+
219+
if strings.HasPrefix(str, "yaml: ") {
220+
p = strings.SplitN(str, ":", 3)
221+
i, _ = strconv.Atoi(strings.Replace(p[1], " line ", "", -1))
222+
str = strings.TrimSpace(p[2])
223+
}
224+
225+
if strings.HasPrefix(str, "template: test:") {
226+
p = strings.SplitN(str, ":", 4)
227+
i, _ = strconv.Atoi(p[2])
228+
str = strings.TrimSpace(p[3])
229+
}
230+
231+
return i, "Templating error: " + str
232+
}

0 commit comments

Comments
 (0)