From 827ee38dcc9850ab46042cbf0d75b9226a8ade81 Mon Sep 17 00:00:00 2001 From: Benjamin Bennett Date: Tue, 2 Aug 2022 14:02:06 +0100 Subject: [PATCH] Amend diagnostic messages and add logging (#399) Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/399 --- internal/privatestate/data.go | 12 +++++++++--- internal/privatestate/data_test.go | 10 +++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/internal/privatestate/data.go b/internal/privatestate/data.go index 5e59db96a..e32876ad5 100644 --- a/internal/privatestate/data.go +++ b/internal/privatestate/data.go @@ -7,6 +7,8 @@ import ( "strings" "unicode/utf8" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/hashicorp/terraform-plugin-framework/diag" ) @@ -132,18 +134,22 @@ func (d ProviderData) SetKey(ctx context.Context, key string, value []byte) diag } if !utf8.Valid(value) { + tflog.Error(ctx, "error calling SetKey with invalid UTF-8 value", map[string]interface{}{"key": key, "value": value}) + diags.AddError("UTF-8 Invalid", "Values stored in private state must be valid UTF-8\n\n"+ - "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", + fmt.Sprintf("The value being supplied for key %q is invalid. Please check the value you are supplying is valid UTF-8.", key), ) return diags } if !json.Valid(value) { + tflog.Error(ctx, "error calling SetKey with invalid JSON value", map[string]interface{}{"key": key, "value": value}) + diags.AddError("JSON Invalid", "Values stored in private state must be valid JSON\n\n"+ - "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", + fmt.Sprintf("The value being supplied for key %q is invalid. Please check the value you are supplying is valid JSON.", key), ) return diags @@ -163,7 +169,7 @@ func ValidateProviderDataKey(ctx context.Context, key string) diag.Diagnostics { diag.NewErrorDiagnostic( "Restricted Resource Private State Namespace", "Using a period ('.') as a prefix for a key used in private state is not allowed\n\n"+ - "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", + fmt.Sprintf("The key %q is invalid. Please check the key you are supplying does not use a a period ('.') as a prefix.", key), ), } } diff --git a/internal/privatestate/data_test.go b/internal/privatestate/data_test.go index 584ada659..7f0f0b1f2 100644 --- a/internal/privatestate/data_test.go +++ b/internal/privatestate/data_test.go @@ -151,7 +151,7 @@ func TestProviderData_GetKey(t *testing.T) { diag.NewErrorDiagnostic( "Restricted Resource Private State Namespace", "Using a period ('.') as a prefix for a key used in private state is not allowed\n\n"+ - "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", + `The key ".key" is invalid. Please check the key you are supplying does not use a a period ('.') as a prefix.`, ), }, }, @@ -204,7 +204,7 @@ func TestProviderData_SetKey(t *testing.T) { diag.NewErrorDiagnostic( "Restricted Resource Private State Namespace", "Using a period ('.') as a prefix for a key used in private state is not allowed\n\n"+ - "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", + `The key ".key" is invalid. Please check the key you are supplying does not use a a period ('.') as a prefix.`, ), }, }, @@ -215,7 +215,7 @@ func TestProviderData_SetKey(t *testing.T) { diag.NewErrorDiagnostic( "UTF-8 Invalid", "Values stored in private state must be valid UTF-8\n\n"+ - "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", + `The value being supplied for key "key" is invalid. Please check the value you are supplying is valid UTF-8.`, ), }, }, @@ -226,7 +226,7 @@ func TestProviderData_SetKey(t *testing.T) { diag.NewErrorDiagnostic( "JSON Invalid", "Values stored in private state must be valid JSON\n\n"+ - "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", + `The value being supplied for key "key" is invalid. Please check the value you are supplying is valid JSON.`, ), }, }, @@ -261,7 +261,7 @@ func TestValidateProviderDataKey(t *testing.T) { expected: diag.Diagnostics{diag.NewErrorDiagnostic( "Restricted Resource Private State Namespace", "Using a period ('.') as a prefix for a key used in private state is not allowed\n\n"+ - "This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.", + `The key ".restricted" is invalid. Please check the key you are supplying does not use a a period ('.') as a prefix.`, )}, }, "namespace-ok": {