opentofu/internal/states/statefile/diagnostics.go
Martin Atkins f40800b3a4 Move states/ to internal/states/
This is part of a general effort to move all of Terraform's non-library
package surface under internal in order to reinforce that these are for
internal use within Terraform only.

If you were previously importing packages under this prefix into an
external codebase, you could pin to an earlier release tag as an interim
solution until you've make a plan to achieve the same functionality some
other way.
2021-05-17 14:09:07 -07:00

63 lines
1.9 KiB
Go

package statefile
import (
"encoding/json"
"fmt"
"github.com/hashicorp/terraform/internal/tfdiags"
)
const invalidFormat = "Invalid state file format"
// jsonUnmarshalDiags is a helper that translates errors returned from
// json.Unmarshal into hopefully-more-helpful diagnostics messages.
func jsonUnmarshalDiags(err error) tfdiags.Diagnostics {
var diags tfdiags.Diagnostics
if err == nil {
return diags
}
switch tErr := err.(type) {
case *json.SyntaxError:
// We've usually already successfully parsed a source file as JSON at
// least once before we'd use jsonUnmarshalDiags with it (to sniff
// the version number) so this particular error should not appear much
// in practice.
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
invalidFormat,
fmt.Sprintf("The state file could not be parsed as JSON: syntax error at byte offset %d.", tErr.Offset),
))
case *json.UnmarshalTypeError:
// This is likely to be the most common area, describing a
// non-conformance between the file and the expected file format
// at a semantic level.
if tErr.Field != "" {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
invalidFormat,
fmt.Sprintf("The state file field %q has invalid value %s", tErr.Field, tErr.Value),
))
break
} else {
// Without a field name, we can't really say anything helpful.
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
invalidFormat,
"The state file does not conform to the expected JSON data structure.",
))
}
default:
// Fallback for all other types of errors. This can happen only for
// custom UnmarshalJSON implementations, so should be encountered
// only rarely.
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
invalidFormat,
fmt.Sprintf("The state file does not conform to the expected JSON data structure: %s.", err.Error()),
))
}
return diags
}