7 "github.com/hashicorp/terraform/tfdiags"
10 const invalidFormat = "Invalid state file format"
12 // jsonUnmarshalDiags is a helper that translates errors returned from
13 // json.Unmarshal into hopefully-more-helpful diagnostics messages.
14 func jsonUnmarshalDiags(err error) tfdiags.Diagnostics {
15 var diags tfdiags.Diagnostics
20 switch tErr := err.(type) {
21 case *json.SyntaxError:
22 // We've usually already successfully parsed a source file as JSON at
23 // least once before we'd use jsonUnmarshalDiags with it (to sniff
24 // the version number) so this particular error should not appear much
26 diags = diags.Append(tfdiags.Sourceless(
29 fmt.Sprintf("The state file could not be parsed as JSON: syntax error at byte offset %d.", tErr.Offset),
31 case *json.UnmarshalTypeError:
32 // This is likely to be the most common area, describing a
33 // non-conformance between the file and the expected file format
34 // at a semantic level.
36 diags = diags.Append(tfdiags.Sourceless(
39 fmt.Sprintf("The state file field %q has invalid value %s", tErr.Field, tErr.Value),
43 // Without a field name, we can't really say anything helpful.
44 diags = diags.Append(tfdiags.Sourceless(
47 "The state file does not conform to the expected JSON data structure.",
51 // Fallback for all other types of errors. This can happen only for
52 // custom UnmarshalJSON implementations, so should be encountered
54 diags = diags.Append(tfdiags.Sourceless(
57 fmt.Sprintf("The state file does not conform to the expected JSON data structure: %s.", err.Error()),