@@ -7,8 +7,19 @@ import (
77 "github.com/zclconf/go-cty/cty"
88)
99
10+ // Diagnostics is a collection of Diagnostic.
11+ //
12+ // Developers should append and build the list of diagnostics up until a fatal
13+ // error is reached, at which point they should return the Diagnostics to the
14+ // SDK.
1015type Diagnostics []Diagnostic
1116
17+ // HasError returns true is Diagnostics contains an instance of
18+ // Severity == Error.
19+ //
20+ // This helper aims to mimic the go error practices of if err != nil. After any
21+ // operation that returns Diagnostics, check that it HasError and bubble up the
22+ // stack.
1223func (diags Diagnostics ) HasError () bool {
1324 for i := range diags {
1425 if diags [i ].Severity == Error {
@@ -18,13 +29,44 @@ func (diags Diagnostics) HasError() bool {
1829 return false
1930}
2031
32+ // Diagnostic is a contextual message intended at outlining problems in user
33+ // configuration.
34+ //
35+ // It supports multiple levels of severity (Error or Warning), a short Summary
36+ // of the problem, an optional longer Detail message that can assist the user in
37+ // fixing the problem, as well as an AttributePath representation which
38+ // Terraform uses to indicate where the issue took place in the user's
39+ // configuration.
40+ //
41+ // A Diagnostic will typically be used to pinpoint a problem with user
42+ // configuration, however it can still be used to present warnings or errors
43+ // to the user without any AttributePath set.
2144type Diagnostic struct {
22- Severity Severity
23- Summary string
24- Detail string
45+ // Severity indicates the level of the Diagnostic. Currently can be set to
46+ // either Error or Warning
47+ Severity Severity
48+ // Summary is a short description of the problem, rendered above location
49+ // information
50+ Summary string
51+ // Detail is an optional second message rendered below location information
52+ // typically used to communicate a potential fix to the user.
53+ Detail string
54+ // AttributePath is a representation of the path starting from the root of
55+ // block (resource, datasource, provider) under evaluation by the SDK, to
56+ // the attribute that the Diagnostic should be associated to.
57+ //
58+ // It is represented with cty.Path, the SDK currently only supports path
59+ // steps of either cty.GetAttrStep (an actual attribute) or cty.IndexStep
60+ // (a step with Key of cty.StringVal for map indexes, and cty.NumberVal
61+ // list indexes, pathing into sets is not currently supported). Please see
62+ // go-cty documentation for more information.
63+ //
64+ // Terraform will use this information to render information on where the
65+ // problem took place in the user's configuration.
2566 AttributePath cty.Path
2667}
2768
69+ // Validate ensures a valid Severity and a non-empty Summary are set.
2870func (d Diagnostic ) Validate () error {
2971 var validSev bool
3072 for _ , sev := range severities {
@@ -37,18 +79,24 @@ func (d Diagnostic) Validate() error {
3779 return fmt .Errorf ("invalid severity: %v" , d .Severity )
3880 }
3981 if d .Summary == "" {
40- return errors .New ("empty detail " )
82+ return errors .New ("empty summary " )
4183 }
4284 return nil
4385}
4486
87+ // FromErr will convert an error into a Diagnostic
4588func FromErr (err error ) Diagnostic {
4689 return Diagnostic {
4790 Severity : Error ,
4891 Summary : err .Error (),
4992 }
5093}
5194
95+ // Severity is an enum type marking the severity level of a Diagnostic
96+ //
97+ // Valid levels are
98+ // - Error
99+ // - Warning
52100type Severity int
53101
54102const (
0 commit comments