Skip to content

Commit 071f904

Browse files
committed
panic invalid sdk diagnostics when building out proto diagnostics
1 parent 719d14d commit 071f904

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

diag/diagnostic.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package diag
22

33
import (
4+
"fmt"
5+
46
"github.com/zclconf/go-cty/cty"
57
)
68

@@ -22,6 +24,23 @@ type Diagnostic struct {
2224
AttributePath cty.Path
2325
}
2426

27+
func (d Diagnostic) Validate() error {
28+
var validSev bool
29+
for _, sev := range severities {
30+
if d.Severity == sev {
31+
validSev = true
32+
break
33+
}
34+
}
35+
if !validSev {
36+
return fmt.Errorf("invalid severity: %v", d.Severity)
37+
}
38+
if d.Summary == "" {
39+
return fmt.Errorf("empty detail")
40+
}
41+
return nil
42+
}
43+
2544
func FromErr(err error) Diagnostic {
2645
return Diagnostic{
2746
Severity: Error,
@@ -35,3 +54,5 @@ const (
3554
Error Severity = iota
3655
Warning
3756
)
57+
58+
var severities = []Severity{Error, Warning}

internal/plugin/convert/diagnostics.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package convert
22

33
import (
4+
"fmt"
5+
46
"github.com/hashicorp/go-cty/cty"
57

68
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -65,6 +67,9 @@ func ProtoToDiags(ds []*proto.Diagnostic) diag.Diagnostics {
6567
func DiagsToProto(diags diag.Diagnostics) []*proto.Diagnostic {
6668
var ds []*proto.Diagnostic
6769
for _, d := range diags {
70+
if err := d.Validate(); err != nil {
71+
panic(fmt.Errorf("Invalid diagnostic: %s. This is always a bug in the provider implementation", err))
72+
}
6873
protoDiag := &proto.Diagnostic{
6974
Summary: d.Summary,
7075
Detail: d.Detail,

0 commit comments

Comments
 (0)