Skip to content

Commit

Permalink
Augment expression parsing error with position (#1887)
Browse files Browse the repository at this point in the history
* Augment expression parsing error with position;
* Remove `YAML parsing error: ` prefix.

```yaml
vars:
  project_id: $(6 + 4) # Case #1
  hello: ((world+))    # Case #2
```

```sh
YAML parsing error: only traversal expressions are supported, got "6 + 4"
YAML parsing error: :0,6-6: Missing expression; Expected the start of an expression, but found the end of the file.

Case #1
Error: only traversal expressions are supported, got "6 + 4"
2:   project_id: $(6 + 4)
Case #2
Error: :0,6-6: Missing expression; Expected the start of an expression, but found the end of the file.
2:   hello: ((world+))
```
  • Loading branch information
mr0re1 authored Oct 26, 2023
1 parent 33bf402 commit 882a913
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"regexp"
"strconv"

"github.com/pkg/errors"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
ctyJson "github.com/zclconf/go-cty/cty/json"
Expand Down Expand Up @@ -72,7 +73,7 @@ func importBlueprint(f string) (Blueprint, YamlCtx, error) {
if yep.pos.Line != 0 {
yamlCtx.pathToPos[yPath(path.String())] = yep.pos
}
errs.At(path, fmt.Errorf("YAML parsing error: %s", yep.errMsg))
errs.At(path, errors.New(yep.errMsg))
}
return Blueprint{}, yamlCtx, errs
}
Expand Down Expand Up @@ -152,7 +153,7 @@ func NewYamlCtx(data []byte) (YamlCtx, error) {
if yep.pos.Line != 0 {
m[yPath(path.String())] = yep.pos
}
errs.At(path, fmt.Errorf("YAML parsing error: %s", yep.errMsg))
errs.At(path, errors.New(yep.errMsg))
}
return YamlCtx{m, lines}, errs
}
Expand Down Expand Up @@ -248,7 +249,7 @@ func (y *YamlValue) unmarshalScalar(n *yaml.Node) error {
}
ty, err := gocty.ImpliedType(s)
if err != nil {
return err
return fmt.Errorf("line %d: %w", n.Line, err)
}
if y.v, err = gocty.ToCtyValue(s, ty); err != nil {
return err
Expand All @@ -257,13 +258,15 @@ func (y *YamlValue) unmarshalScalar(n *yaml.Node) error {
if l, is := IsYamlExpressionLiteral(y.v); is { // HCL literal
var e Expression
if e, err = ParseExpression(l); err != nil {
return err
// TODO: point to exact location within expression, see Diagnostic.Subject
return fmt.Errorf("line %d: %w", n.Line, err)
}
y.v = e.AsValue()
} else if y.v.Type() == cty.String && hasVariable(y.v.AsString()) { // "simple" variable
e, err := SimpleVarToExpression(y.v.AsString())
if err != nil {
return err
// TODO: point to exact location within expression, see Diagnostic.Subject
return fmt.Errorf("line %d: %w", n.Line, err)
}
y.v = e.AsValue()
}
Expand Down

0 comments on commit 882a913

Please sign in to comment.