From 87e1fb4d66189e2c3076d902c935c163977e3831 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Wed, 6 Dec 2017 16:44:24 -0800 Subject: [PATCH] config: a nicer error message for invalid provider constraints Previously our error message here was confusing and redundant: Error starting operation: provider.null: invalid version constraint "not valid": Malformed constraint: not valid Instead, we'll generate a full HCL2 diagnostic here, which results in something (subjectively) nicer: Error: Invalid provider version constraint The value "@ 1.0.0" given for provider.null is not a valid version constraint. At the moment this message is an outlier in that the other validation errors are all still just plain Go errors, but over time we'll want to adjust all of these to be full diagnostics so that we can embed source range information in them to help the user find the offending configuration. --- config/config.go | 16 ++++++++++++---- config/config_test.go | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 2a62eb2b9..055a7f330 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ import ( "strconv" "strings" + hcl2 "github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/hil/ast" "github.com/hashicorp/terraform/helper/hilmapstructure" "github.com/hashicorp/terraform/plugin/discovery" @@ -415,10 +416,17 @@ func (c *Config) Validate() tfdiags.Diagnostics { if p.Version != "" { _, err := discovery.ConstraintStr(p.Version).Parse() if err != nil { - diags = diags.Append(fmt.Errorf( - "provider.%s: invalid version constraint %q: %s", - name, p.Version, err, - )) + diags = diags.Append(&hcl2.Diagnostic{ + Severity: hcl2.DiagError, + Summary: "Invalid provider version constraint", + Detail: fmt.Sprintf( + "The value %q given for provider.%s is not a valid version constraint.", + p.Version, name, + ), + // TODO: include a "Subject" source reference in here, + // once the config loader is able to retain source + // location information. + }) } } diff --git a/config/config_test.go b/config/config_test.go index 18bab3f17..6f6e7a468 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -217,7 +217,7 @@ func TestConfigValidate_table(t *testing.T) { "provider with invalid version constraint", "provider-version-invalid", true, - "invalid version constraint", + "not a valid version constraint", }, { "invalid provider name in module block",