Skip to content

Commit

Permalink
Amend diagnostic messages and add logging (#399)
Browse files Browse the repository at this point in the history
Reference: #399
  • Loading branch information
bendbennett committed Aug 2, 2022
1 parent b73cbd9 commit 827ee38
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 9 additions & 3 deletions internal/privatestate/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"unicode/utf8"

"github.com/hashicorp/terraform-plugin-log/tflog"

"github.com/hashicorp/terraform-plugin-framework/diag"
)

Expand Down Expand Up @@ -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
Expand All @@ -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),
),
}
}
Expand Down
10 changes: 5 additions & 5 deletions internal/privatestate/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.`,
),
},
},
Expand Down Expand Up @@ -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.`,
),
},
},
Expand All @@ -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.`,
),
},
},
Expand All @@ -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.`,
),
},
},
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit 827ee38

Please sign in to comment.