From 6e72de9b49307f5b291e4e22ae320ba7b73b7fb4 Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Wed, 6 Mar 2024 17:47:55 +0100 Subject: [PATCH] improve diag handling --- pkg/tfshim/sdk-v2/upgrade_state.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/tfshim/sdk-v2/upgrade_state.go b/pkg/tfshim/sdk-v2/upgrade_state.go index a26dddc754..2ef4755c83 100644 --- a/pkg/tfshim/sdk-v2/upgrade_state.go +++ b/pkg/tfshim/sdk-v2/upgrade_state.go @@ -2,13 +2,13 @@ package sdkv2 import ( "context" - "errors" "fmt" "strconv" "github.com/hashicorp/go-cty/cty" "github.com/hashicorp/go-cty/cty/msgpack" "github.com/hashicorp/terraform-plugin-go/tfprotov5" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) @@ -99,17 +99,16 @@ func upgradeResourceStateRPC( } // Handle returned diagnostics. + var dd diag.Diagnostics for _, d := range resp.Diagnostics { - msg := fmt.Sprintf("%s: %s", d.Summary, d.Detail) - switch d.Severity { - case tfprotov5.DiagnosticSeverityError: - err = errors.Join(err, d.Attribute.NewError(fmt.Errorf("%s", msg))) - case tfprotov5.DiagnosticSeverityWarning: - // Accessing the logger (GetLogger) requires an import cycle on - // tfbridge, so ignore for now. + if d == nil { + continue } + rd := recoverDiagnostic(*d) + dd = append(dd, rd) + logDiag(ctx, rd) } - if err != nil { + if err := diagToError(dd); err != nil { return cty.Value{}, err }