User-friendlier errors

This commit is contained in:
Nils Hanke 2022-11-16 16:33:51 +01:00 committed by Nils Hanke
parent e1d8926395
commit 6e5895f200
3 changed files with 31 additions and 4 deletions

View file

@ -10,7 +10,6 @@ import (
"bytes"
"embed"
"errors"
"fmt"
"io/fs"
"path"
"path/filepath"
@ -21,6 +20,9 @@ import (
"github.com/spf13/afero"
)
// ErrTerraformWorkspaceDifferentFiles is returned when a re-used existing Terraform workspace has different files than the ones to be extracted (e.g. due to a version mix-up or incomplete writes).
var ErrTerraformWorkspaceDifferentFiles = errors.New("creating cluster: trying to overwrite an existing Terraform file with a different version")
//go:embed terraform/*
//go:embed terraform/*/.terraform.lock.hcl
var terraformFS embed.FS
@ -53,7 +55,7 @@ func prepareWorkspace(fileHandler file.Handler, provider cloudprovider.Provider,
}
if !bytes.Equal(content, existingFileContent) {
return fmt.Errorf("trying to overwrite existing Terraform file with different version")
return ErrTerraformWorkspaceDifferentFiles
}
return nil
} else if err != nil {

View file

@ -29,6 +29,9 @@ const (
terraformVarsFile = "terraform.tfvars"
)
// ErrTerraformWorkspaceExistsWithDifferentVariables is returned when existing Terraform files differ from the version the CLI wants to extract.
var ErrTerraformWorkspaceExistsWithDifferentVariables = errors.New("creating cluster: a Terraform workspace already exists with different variables")
// Client manages interaction with Terraform.
type Client struct {
tf tfInterface
@ -162,7 +165,7 @@ func (c *Client) writeVars(vars Variables) error {
return err
}
if vars.String() != string(varsContent) {
return errors.New("creating cluster: workspace already exists with different variables")
return ErrTerraformWorkspaceExistsWithDifferentVariables
}
} else if err != nil {
return err