From 864aa9d1d629090cfc4ddf9fdd344d34dee9793e Mon Sep 17 00:00:00 2001 From: Denis O Date: Mon, 15 Jul 2024 10:58:43 +0100 Subject: [PATCH] Error handling fixes (#1816) Signed-off-by: Denis O --- internal/backend/remote-state/azure/client.go | 3 --- internal/cloud/tfe_client_mock.go | 8 ++++---- .../cloudplugin/cloudplugin1/grpc_client.go | 2 +- internal/command/state_meta.go | 3 --- internal/command/workspace_select.go | 5 ----- internal/communicator/ssh/communicator.go | 2 +- internal/legacy/helper/schema/core_schema.go | 2 +- internal/tofu/graph_dot_test.go | 17 ----------------- .../tofu/node_resource_abstract_instance.go | 6 +----- 9 files changed, 8 insertions(+), 40 deletions(-) diff --git a/internal/backend/remote-state/azure/client.go b/internal/backend/remote-state/azure/client.go index 1dd9eb78d8..bfa064342d 100644 --- a/internal/backend/remote-state/azure/client.go +++ b/internal/backend/remote-state/azure/client.go @@ -230,9 +230,6 @@ func (c *RemoteClient) writeLockInfo(info *statemgr.LockInfo) error { if err != nil { return err } - if err != nil { - return err - } if info == nil { delete(blob.MetaData, lockInfoMetaKey) diff --git a/internal/cloud/tfe_client_mock.go b/internal/cloud/tfe_client_mock.go index 5b8b00f00b..8a2341480e 100644 --- a/internal/cloud/tfe_client_mock.go +++ b/internal/cloud/tfe_client_mock.go @@ -1034,18 +1034,18 @@ func (m *MockProjects) Update(ctx context.Context, projectID string, options tfe } func (m *MockProjects) Delete(ctx context.Context, projectID string) error { - var p *tfe.Project = nil + var projectToDelete *tfe.Project for _, p := range m.projects { if p.ID == projectID { - + projectToDelete = p break } } - if p == nil { + if projectToDelete == nil { return tfe.ErrResourceNotFound } - delete(m.projects, p.Name) + delete(m.projects, projectToDelete.Name) return nil } diff --git a/internal/cloudplugin/cloudplugin1/grpc_client.go b/internal/cloudplugin/cloudplugin1/grpc_client.go index 9a2c8df073..7c070f8a92 100644 --- a/internal/cloudplugin/cloudplugin1/grpc_client.go +++ b/internal/cloudplugin/cloudplugin1/grpc_client.go @@ -55,7 +55,7 @@ func (c GRPCCloudClient) Execute(args []string, stdout, stderr io.Writer) int { return 1 } } else if bytes := response.GetStderr(); len(bytes) > 0 { - fmt.Fprint(stderr, string(bytes)) + _, err := fmt.Fprint(stderr, string(bytes)) if err != nil { log.Printf("[ERROR] Failed to write cloudplugin output to stderr: %s", err) return 1 diff --git a/internal/command/state_meta.go b/internal/command/state_meta.go index f047b7870f..687f54445f 100644 --- a/internal/command/state_meta.go +++ b/internal/command/state_meta.go @@ -69,9 +69,6 @@ func (c *StateMeta) State(enc encryption.Encryption) (statemgr.Full, error) { } localB := localRaw.(*backendLocal.Local) _, stateOutPath, _ = localB.StatePaths(workspace) - if err != nil { - return nil, err - } realState = s } diff --git a/internal/command/workspace_select.go b/internal/command/workspace_select.go index 576d1f47fa..8bc25bf95c 100644 --- a/internal/command/workspace_select.go +++ b/internal/command/workspace_select.go @@ -79,11 +79,6 @@ func (c *WorkspaceSelectCommand) Run(args []string) int { return 1 } - if err != nil { - c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err)) - return 1 - } - // This command will not write state c.ignoreRemoteVersionConflict(b) diff --git a/internal/communicator/ssh/communicator.go b/internal/communicator/ssh/communicator.go index 3445880c3f..ffe666cfb2 100644 --- a/internal/communicator/ssh/communicator.go +++ b/internal/communicator/ssh/communicator.go @@ -718,7 +718,7 @@ func scpUploadDirProtocol(name string, w io.Writer, r *bufio.Reader, f func() er return err } - fmt.Fprintln(w, "E") + _, err = fmt.Fprintln(w, "E") if err != nil { return err } diff --git a/internal/legacy/helper/schema/core_schema.go b/internal/legacy/helper/schema/core_schema.go index 930950c159..399da1e83d 100644 --- a/internal/legacy/helper/schema/core_schema.go +++ b/internal/legacy/helper/schema/core_schema.go @@ -114,7 +114,7 @@ func (s *Schema) coreConfigSchemaAttribute() *configschema.Attribute { // "Required" to false and let the provider try calling its // DefaultFunc again during the validate step, where it can then // return the error. - if err != nil || (err == nil && v != nil) { + if err != nil || v != nil { reqd = false opt = true } diff --git a/internal/tofu/graph_dot_test.go b/internal/tofu/graph_dot_test.go index 01cc513a12..fca6cbde74 100644 --- a/internal/tofu/graph_dot_test.go +++ b/internal/tofu/graph_dot_test.go @@ -18,7 +18,6 @@ func TestGraphDot(t *testing.T) { Graph testGraphFunc Opts dag.DotOpts Expect string - Error string }{ { Name: "empty", @@ -236,23 +235,7 @@ digraph { tn := tc.Name t.Run(tn, func(t *testing.T) { g := tc.Graph() - var err error - //actual, err := GraphDot(g, &tc.Opts) actual := string(g.Dot(&tc.Opts)) - - if err == nil && tc.Error != "" { - t.Fatalf("%s: expected err: %s, got none", tn, tc.Error) - } - if err != nil && tc.Error == "" { - t.Fatalf("%s: unexpected err: %s", tn, err) - } - if err != nil && tc.Error != "" { - if !strings.Contains(err.Error(), tc.Error) { - t.Fatalf("%s: expected err: %s\nto contain: %s", tn, err, tc.Error) - } - return - } - expected := strings.TrimSpace(tc.Expect) + "\n" if actual != expected { t.Fatalf("%s:\n\nexpected:\n%s\n\ngot:\n%s", tn, expected, actual) diff --git a/internal/tofu/node_resource_abstract_instance.go b/internal/tofu/node_resource_abstract_instance.go index d7afa1a33a..4cf17cb62e 100644 --- a/internal/tofu/node_resource_abstract_instance.go +++ b/internal/tofu/node_resource_abstract_instance.go @@ -326,11 +326,7 @@ func (n *NodeAbstractResourceInstance) writeResourceInstanceStateImpl(ctx EvalCo return nil } - if obj != nil { - log.Printf("[TRACE] %s: writing state object for %s", logFuncName, absAddr) - } else { - log.Printf("[TRACE] %s: removing state object for %s", logFuncName, absAddr) - } + log.Printf("[TRACE] %s: writing state object for %s", logFuncName, absAddr) schema, currentVersion := providerSchema.SchemaForResourceAddr(absAddr.ContainingResource().Resource) if schema == nil {