Skip to content

Commit f8cc3a2

Browse files
committed
tfsdk: Return attr.Value being nil as its own error diagnostic
1 parent 22dcfaa commit f8cc3a2

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

tfsdk/config.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,17 @@ func (c Config) Get(ctx context.Context, target interface{}) diag.Diagnostics {
2626
func (c Config) GetAttribute(ctx context.Context, path *tftypes.AttributePath, target interface{}) diag.Diagnostics {
2727
attrValue, diags := c.getAttributeValue(ctx, path)
2828

29-
if attrValue == nil || diags.HasError() {
29+
if diags.HasError() {
30+
return diags
31+
}
32+
33+
if attrValue == nil {
34+
diags.AddAttributeError(
35+
path,
36+
"Config Read Error",
37+
"An unexpected error was encountered trying to read an attribute from the configuration. This is always an error in the provider. Please report the following to the provider developer:\n\n"+
38+
"Missing attribute value, however no error was returned. Preventing the panic from this situation.",
39+
)
3040
return diags
3141
}
3242

tfsdk/plan.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,17 @@ func (p Plan) Get(ctx context.Context, target interface{}) diag.Diagnostics {
2626
func (p Plan) GetAttribute(ctx context.Context, path *tftypes.AttributePath, target interface{}) diag.Diagnostics {
2727
attrValue, diags := p.getAttributeValue(ctx, path)
2828

29-
if attrValue == nil || diags.HasError() {
29+
if diags.HasError() {
30+
return diags
31+
}
32+
33+
if attrValue == nil {
34+
diags.AddAttributeError(
35+
path,
36+
"Plan Read Error",
37+
"An unexpected error was encountered trying to read an attribute from the plan. This is always an error in the provider. Please report the following to the provider developer:\n\n"+
38+
"Missing attribute value, however no error was returned. Preventing the panic from this situation.",
39+
)
3040
return diags
3141
}
3242

tfsdk/state.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,17 @@ func (s State) Get(ctx context.Context, target interface{}) diag.Diagnostics {
2626
func (s State) GetAttribute(ctx context.Context, path *tftypes.AttributePath, target interface{}) diag.Diagnostics {
2727
attrValue, diags := s.getAttributeValue(ctx, path)
2828

29-
if attrValue == nil || diags.HasError() {
29+
if diags.HasError() {
30+
return diags
31+
}
32+
33+
if attrValue == nil {
34+
diags.AddAttributeError(
35+
path,
36+
"State Read Error",
37+
"An unexpected error was encountered trying to read an attribute from the state. This is always an error in the provider. Please report the following to the provider developer:\n\n"+
38+
"Missing attribute value, however no error was returned. Preventing the panic from this situation.",
39+
)
3040
return diags
3141
}
3242

0 commit comments

Comments
 (0)