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.
This commit is contained in:
Martin Atkins 2017-12-06 16:44:24 -08:00
parent 9a5c865040
commit 87e1fb4d66
2 changed files with 13 additions and 5 deletions

View File

@ -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.
})
}
}

View File

@ -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",