Skip to content

Commit 9419032

Browse files
committed
thread through error handling
1 parent 5811f33 commit 9419032

File tree

7 files changed

+230
-333
lines changed

7 files changed

+230
-333
lines changed

helper/convert/type.go

Lines changed: 0 additions & 54 deletions
This file was deleted.

helper/convert/type_test.go

Lines changed: 0 additions & 122 deletions
This file was deleted.

helper/convert/value.go

Lines changed: 0 additions & 134 deletions
This file was deleted.

helper/schema/resource_data.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717

1818
"github.com/hashicorp/terraform-plugin-go/tftypes"
1919
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
20-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/convert"
2120
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/configschema"
2221
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/configs/hcl2shim"
22+
"github.com/hashicorp/terraform-plugin-sdk/v2/internal/plugin/convert"
2323
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2424
)
2525

@@ -64,25 +64,25 @@ type getResult struct {
6464
}
6565

6666
// TfTypeIdentityState returns the identity data as a tftypes.Value.
67-
func (d *ResourceData) TfTypeIdentityState() tftypes.Value {
67+
func (d *ResourceData) TfTypeIdentityState() (*tftypes.Value, error) {
6868
s := schemaMap(d.identitySchema).CoreConfigSchema()
6969

7070
state := d.State()
7171

7272
if state == nil {
73-
log.Panicf("state is nil")
73+
return nil, fmt.Errorf("state is nil, call SetId() on ResourceData first")
7474
}
7575

7676
stateVal, err := hcl2shim.HCL2ValueFromFlatmap(state.Identity, s.ImpliedType())
7777
if err != nil {
78-
log.Panicf("converting identity to tf value: %+v", err)
78+
return nil, fmt.Errorf("converting identity flatmap to cty value: %+v", err)
7979
}
8080

8181
return convert.ToTfValue(stateVal)
8282
}
8383

8484
// TfTypeResourceState returns the resource data as a tftypes.Value.
85-
func (d *ResourceData) TfTypeResourceState() tftypes.Value {
85+
func (d *ResourceData) TfTypeResourceState() (*tftypes.Value, error) {
8686
s := schemaMap(d.schema).CoreConfigSchema()
8787

8888
// The CoreConfigSchema method on schemaMaps doesn't automatically handle adding the id
@@ -145,14 +145,13 @@ func (d *ResourceData) TfTypeResourceState() tftypes.Value {
145145
}
146146

147147
state := d.State()
148-
149148
if state == nil {
150-
log.Panicf("state is nil")
149+
return nil, fmt.Errorf("state is nil, call SetId() on ResourceData first")
151150
}
152151

153152
stateVal, err := hcl2shim.HCL2ValueFromFlatmap(state.Attributes, s.ImpliedType())
154153
if err != nil {
155-
log.Panicf("converting resource state to tf value: %+v", err)
154+
return nil, fmt.Errorf("converting resource state flatmap to cty value: %+v", err)
156155
}
157156

158157
return convert.ToTfValue(stateVal)

0 commit comments

Comments
 (0)