Skip to content

Commit

Permalink
🔊 Annotate yaml when an error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jun 1, 2022
1 parent 2491cdc commit 59279a7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
10 changes: 8 additions & 2 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/clevyr/go-yampl/internal/config"
"github.com/clevyr/go-yampl/internal/node"
"github.com/clevyr/go-yampl/internal/parser"
"github.com/clevyr/go-yampl/internal/visitor"
"github.com/goccy/go-yaml/ast"
Expand Down Expand Up @@ -152,8 +153,13 @@ func templateReader(conf config.Config, r io.Reader) (string, error) {

v := visitor.NewTemplateComments(conf)
for _, doc := range file.Docs {
if ast.Walk(&v, doc.Body); v.Error() != nil {
return "", v.Error()
ast.Walk(&v, doc.Body)
if err := v.Error(); err != nil {
switch err := err.(type) {
case node.PrintableError:
return "", fmt.Errorf("%w\n%v", err, err.AnnotateSource(file.String(), colored))
}
return "", err
}
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/flag_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
var (
logLevel string
logFormat string
colored bool
)

func init() {
Expand Down Expand Up @@ -54,8 +55,9 @@ func initLogFormat(format string) log.Formatter {
var formatter log.Formatter = &log.TextFormatter{}
switch format {
case "auto", "a":
break
colored = true
case "color", "c":
colored = true
formatter.(*log.TextFormatter).ForceColors = true
case "plain", "p":
formatter.(*log.TextFormatter).DisableColors = true
Expand Down
36 changes: 36 additions & 0 deletions internal/node/printable_error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package node

import (
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
)

func NewPrintableError(err error, node ast.Node) PrintableError {
return PrintableError{
err: err,
node: node,
}
}

type PrintableError struct {
err error
node ast.Node
}

func (p PrintableError) Error() string {
return p.err.Error()
}

func (p PrintableError) AnnotateSource(src string, colored bool) string {
path, err := yaml.PathString(p.node.GetPath())
if err != nil {
return ""
}

source, err := path.AnnotateSource([]byte(src), colored)
if err != nil {
return ""
}

return string(source)
}
8 changes: 4 additions & 4 deletions internal/visitor/template_comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func (v *TemplateComments) Visit(n ast.Node) ast.Visitor {
newNode, err := templateComment(v.conf, comment, n.Value)
if err != nil {
if v.err == nil {
v.err = err
v.err = node.NewPrintableError(err, n)
}
return nil
}

if newNode != nil {
if err := n.Replace(newNode); err != nil {
if v.err == nil {
v.err = err
v.err = node.NewPrintableError(err, n)
}
return nil
}
Expand All @@ -49,15 +49,15 @@ func (v *TemplateComments) Visit(n ast.Node) ast.Visitor {
newNode, err := templateComment(v.conf, comment, value)
if err != nil {
if v.err == nil {
v.err = err
v.err = node.NewPrintableError(err, value)
}
return nil
}

if newNode != nil {
if err := n.Replace(i, newNode); err != nil {
if v.err == nil {
v.err = err
v.err = node.NewPrintableError(err, value)
}
return nil
}
Expand Down

0 comments on commit 59279a7

Please sign in to comment.