File tree Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Expand file tree Collapse file tree 2 files changed +26
-0
lines changed Original file line number Diff line number Diff line change 11package diag
22
33import (
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+
2544func 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 }
Original file line number Diff line number Diff line change 11package convert
22
33import (
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 {
6567func 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 ,
You can’t perform that action at this time.
0 commit comments