From 882a913e7c37f619f6991dfc79fa63baad5e8025 Mon Sep 17 00:00:00 2001 From: Ivan Orlov Date: Thu, 26 Oct 2023 11:34:31 -0700 Subject: [PATCH] Augment expression parsing error with position (#1887) * 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+)) ``` --- pkg/config/yaml.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/pkg/config/yaml.go b/pkg/config/yaml.go index 685e2f707d..b3c2077295 100644 --- a/pkg/config/yaml.go +++ b/pkg/config/yaml.go @@ -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" @@ -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 } @@ -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 } @@ -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 @@ -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() }