Skip to content

Commit

Permalink
Merge pull request #15 from blendle/better-errros
Browse files Browse the repository at this point in the history
better error reporting details to stderr
  • Loading branch information
JeanMertz authored Feb 23, 2018
2 parents 34744c9 + a5420e7 commit 9408029
Showing 1 changed file with 67 additions and 1 deletion.
68 changes: 67 additions & 1 deletion chartsconfig/parser.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package chartsconfig

import (
"bufio"
"bytes"
"errors"
"fmt"
"html/template"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"

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

if err = yaml.Unmarshal(out, m); err != nil {
return nil, err
return nil, wrapError(out, err)
}

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

return tpls, err
}

func wrapError(b []byte, err error) error {
var lines []string
shortLines := []string{"\n"}

el, str := errorLine(err)

scanner := bufio.NewScanner(bytes.NewReader(b))
l := 0
for scanner.Scan() {
l++
}
ll := len(strconv.Itoa(l))

i := 1
scanner = bufio.NewScanner(bytes.NewReader(b))
for scanner.Scan() {
line := fmt.Sprintf("%*d: %s", ll, i, scanner.Text())

// if we know the error line, we create an extra summary of the context
// surrounding the error itself, starting 3 lines before, ending 3 after.
if el != 0 {
if i == el {
line = "\x1b[31;1m" + line + "\x1b[0m"
}

if (i >= el-3) && (i <= el+3) {
shortLines = append(shortLines, line)
}
}

lines = append(lines, line)
i++
}

lines = append(lines, shortLines...)
lines = append(lines, "\n"+str)

return errors.New(strings.Join(lines, "\n"))
}

func errorLine(err error) (int, string) {
var i int
var p []string
str := err.Error()

println(str)

if strings.HasPrefix(str, "yaml: ") {
p = strings.SplitN(str, ":", 3)
i, _ = strconv.Atoi(strings.Replace(p[1], " line ", "", -1))
str = strings.TrimSpace(p[2])
}

if strings.HasPrefix(str, "template: test:") {
p = strings.SplitN(str, ":", 4)
i, _ = strconv.Atoi(p[2])
str = strings.TrimSpace(p[3])
}

return i, "Templating error: " + str
}

0 comments on commit 9408029

Please sign in to comment.