github.com/hashicorp/terraform-plugin-sdk@v1.17.2/internal/tfdiags/diagnostics.go (about) 1 package tfdiags 2 3 import ( 4 "bytes" 5 "fmt" 6 "path/filepath" 7 "sort" 8 "strings" 9 10 "github.com/hashicorp/errwrap" 11 multierror "github.com/hashicorp/go-multierror" 12 "github.com/hashicorp/hcl/v2" 13 ) 14 15 // Diagnostics is a list of diagnostics. Diagnostics is intended to be used 16 // where a Go "error" might normally be used, allowing richer information 17 // to be conveyed (more context, support for warnings). 18 // 19 // A nil Diagnostics is a valid, empty diagnostics list, thus allowing 20 // heap allocation to be avoided in the common case where there are no 21 // diagnostics to report at all. 22 type Diagnostics []Diagnostic 23 24 // Append is the main interface for constructing Diagnostics lists, taking 25 // an existing list (which may be nil) and appending the new objects to it 26 // after normalizing them to be implementations of Diagnostic. 27 // 28 // The usual pattern for a function that natively "speaks" diagnostics is: 29 // 30 // // Create a nil Diagnostics at the start of the function 31 // var diags diag.Diagnostics 32 // 33 // // At later points, build on it if errors / warnings occur: 34 // foo, err := DoSomethingRisky() 35 // if err != nil { 36 // diags = diags.Append(err) 37 // } 38 // 39 // // Eventually return the result and diagnostics in place of error 40 // return result, diags 41 // 42 // Append accepts a variety of different diagnostic-like types, including 43 // native Go errors and HCL diagnostics. It also knows how to unwrap 44 // a multierror.Error into separate error diagnostics. It can be passed 45 // another Diagnostics to concatenate the two lists. If given something 46 // it cannot handle, this function will panic. 47 func (diags Diagnostics) Append(new ...interface{}) Diagnostics { 48 for _, item := range new { 49 if item == nil { 50 continue 51 } 52 53 switch ti := item.(type) { 54 case Diagnostic: 55 diags = append(diags, ti) 56 case Diagnostics: 57 diags = append(diags, ti...) // flatten 58 case diagnosticsAsError: 59 diags = diags.Append(ti.Diagnostics) // unwrap 60 case NonFatalError: 61 diags = diags.Append(ti.Diagnostics) // unwrap 62 case hcl.Diagnostics: 63 for _, hclDiag := range ti { 64 diags = append(diags, hclDiagnostic{hclDiag}) 65 } 66 case *hcl.Diagnostic: 67 diags = append(diags, hclDiagnostic{ti}) 68 case *multierror.Error: 69 for _, err := range ti.Errors { 70 diags = append(diags, nativeError{err}) 71 } 72 case error: 73 switch { 74 case errwrap.ContainsType(ti, Diagnostics(nil)): 75 // If we have an errwrap wrapper with a Diagnostics hiding 76 // inside then we'll unpick it here to get access to the 77 // individual diagnostics. 78 diags = diags.Append(errwrap.GetType(ti, Diagnostics(nil))) 79 case errwrap.ContainsType(ti, hcl.Diagnostics(nil)): 80 // Likewise, if we have HCL diagnostics we'll unpick that too. 81 diags = diags.Append(errwrap.GetType(ti, hcl.Diagnostics(nil))) 82 default: 83 diags = append(diags, nativeError{ti}) 84 } 85 default: 86 panic(fmt.Errorf("can't construct diagnostic(s) from %T", item)) 87 } 88 } 89 90 // Given the above, we should never end up with a non-nil empty slice 91 // here, but we'll make sure of that so callers can rely on empty == nil 92 if len(diags) == 0 { 93 return nil 94 } 95 96 return diags 97 } 98 99 // HasErrors returns true if any of the diagnostics in the list have 100 // a severity of Error. 101 func (diags Diagnostics) HasErrors() bool { 102 for _, diag := range diags { 103 if diag.Severity() == Error { 104 return true 105 } 106 } 107 return false 108 } 109 110 // ForRPC returns a version of the receiver that has been simplified so that 111 // it is friendly to RPC protocols. 112 // 113 // Currently this means that it can be serialized with encoding/gob and 114 // subsequently re-inflated. It may later grow to include other serialization 115 // formats. 116 // 117 // Note that this loses information about the original objects used to 118 // construct the diagnostics, so e.g. the errwrap API will not work as 119 // expected on an error-wrapped Diagnostics that came from ForRPC. 120 func (diags Diagnostics) ForRPC() Diagnostics { 121 ret := make(Diagnostics, len(diags)) 122 for i := range diags { 123 ret[i] = makeRPCFriendlyDiag(diags[i]) 124 } 125 return ret 126 } 127 128 // Err flattens a diagnostics list into a single Go error, or to nil 129 // if the diagnostics list does not include any error-level diagnostics. 130 // 131 // This can be used to smuggle diagnostics through an API that deals in 132 // native errors, but unfortunately it will lose naked warnings (warnings 133 // that aren't accompanied by at least one error) since such APIs have no 134 // mechanism through which to report these. 135 // 136 // return result, diags.Error() 137 func (diags Diagnostics) Err() error { 138 if !diags.HasErrors() { 139 return nil 140 } 141 return diagnosticsAsError{diags} 142 } 143 144 // ErrWithWarnings is similar to Err except that it will also return a non-nil 145 // error if the receiver contains only warnings. 146 // 147 // In the warnings-only situation, the result is guaranteed to be of dynamic 148 // type NonFatalError, allowing diagnostics-aware callers to type-assert 149 // and unwrap it, treating it as non-fatal. 150 // 151 // This should be used only in contexts where the caller is able to recognize 152 // and handle NonFatalError. For normal callers that expect a lack of errors 153 // to be signaled by nil, use just Diagnostics.Err. 154 func (diags Diagnostics) ErrWithWarnings() error { 155 if len(diags) == 0 { 156 return nil 157 } 158 if diags.HasErrors() { 159 return diags.Err() 160 } 161 return NonFatalError{diags} 162 } 163 164 // NonFatalErr is similar to Err except that it always returns either nil 165 // (if there are no diagnostics at all) or NonFatalError. 166 // 167 // This allows diagnostics to be returned over an error return channel while 168 // being explicit that the diagnostics should not halt processing. 169 // 170 // This should be used only in contexts where the caller is able to recognize 171 // and handle NonFatalError. For normal callers that expect a lack of errors 172 // to be signaled by nil, use just Diagnostics.Err. 173 func (diags Diagnostics) NonFatalErr() error { 174 if len(diags) == 0 { 175 return nil 176 } 177 return NonFatalError{diags} 178 } 179 180 type diagnosticsAsError struct { 181 Diagnostics 182 } 183 184 func (dae diagnosticsAsError) Error() string { 185 diags := dae.Diagnostics 186 switch { 187 case len(diags) == 0: 188 // should never happen, since we don't create this wrapper if 189 // there are no diagnostics in the list. 190 return "no errors" 191 case len(diags) == 1: 192 desc := diags[0].Description() 193 if desc.Detail == "" { 194 return desc.Summary 195 } 196 return fmt.Sprintf("%s: %s", desc.Summary, desc.Detail) 197 default: 198 var ret bytes.Buffer 199 fmt.Fprintf(&ret, "%d problems:\n", len(diags)) 200 for _, diag := range dae.Diagnostics { 201 desc := diag.Description() 202 if desc.Detail == "" { 203 fmt.Fprintf(&ret, "\n- %s", desc.Summary) 204 } else { 205 fmt.Fprintf(&ret, "\n- %s: %s", desc.Summary, desc.Detail) 206 } 207 } 208 return ret.String() 209 } 210 } 211 212 // WrappedErrors is an implementation of errwrap.Wrapper so that an error-wrapped 213 // diagnostics object can be picked apart by errwrap-aware code. 214 func (dae diagnosticsAsError) WrappedErrors() []error { 215 var errs []error 216 for _, diag := range dae.Diagnostics { 217 if wrapper, isErr := diag.(nativeError); isErr { 218 errs = append(errs, wrapper.err) 219 } 220 } 221 return errs 222 } 223 224 // NonFatalError is a special error type, returned by 225 // Diagnostics.ErrWithWarnings and Diagnostics.NonFatalErr, 226 // that indicates that the wrapped diagnostics should be treated as non-fatal. 227 // Callers can conditionally type-assert an error to this type in order to 228 // detect the non-fatal scenario and handle it in a different way. 229 type NonFatalError struct { 230 Diagnostics 231 } 232 233 func (woe NonFatalError) Error() string { 234 diags := woe.Diagnostics 235 switch { 236 case len(diags) == 0: 237 // should never happen, since we don't create this wrapper if 238 // there are no diagnostics in the list. 239 return "no errors or warnings" 240 case len(diags) == 1: 241 desc := diags[0].Description() 242 if desc.Detail == "" { 243 return desc.Summary 244 } 245 return fmt.Sprintf("%s: %s", desc.Summary, desc.Detail) 246 default: 247 var ret bytes.Buffer 248 if diags.HasErrors() { 249 fmt.Fprintf(&ret, "%d problems:\n", len(diags)) 250 } else { 251 fmt.Fprintf(&ret, "%d warnings:\n", len(diags)) 252 } 253 for _, diag := range woe.Diagnostics { 254 desc := diag.Description() 255 if desc.Detail == "" { 256 fmt.Fprintf(&ret, "\n- %s", desc.Summary) 257 } else { 258 fmt.Fprintf(&ret, "\n- %s: %s", desc.Summary, desc.Detail) 259 } 260 } 261 return ret.String() 262 } 263 } 264 265 // sortDiagnostics is an implementation of sort.Interface 266 type sortDiagnostics []Diagnostic 267 268 var _ sort.Interface = sortDiagnostics(nil) 269 270 func (sd sortDiagnostics) Len() int { 271 return len(sd) 272 } 273 274 func (sd sortDiagnostics) Less(i, j int) bool { 275 iD, jD := sd[i], sd[j] 276 iSev, jSev := iD.Severity(), jD.Severity() 277 iSrc, jSrc := iD.Source(), jD.Source() 278 279 switch { 280 281 case iSev != jSev: 282 return iSev == Warning 283 284 case (iSrc.Subject == nil) != (jSrc.Subject == nil): 285 return iSrc.Subject == nil 286 287 case iSrc.Subject != nil && *iSrc.Subject != *jSrc.Subject: 288 iSubj := iSrc.Subject 289 jSubj := jSrc.Subject 290 switch { 291 case iSubj.Filename != jSubj.Filename: 292 // Path with fewer segments goes first if they are different lengths 293 sep := string(filepath.Separator) 294 iCount := strings.Count(iSubj.Filename, sep) 295 jCount := strings.Count(jSubj.Filename, sep) 296 if iCount != jCount { 297 return iCount < jCount 298 } 299 return iSubj.Filename < jSubj.Filename 300 case iSubj.Start.Byte != jSubj.Start.Byte: 301 return iSubj.Start.Byte < jSubj.Start.Byte 302 case iSubj.End.Byte != jSubj.End.Byte: 303 return iSubj.End.Byte < jSubj.End.Byte 304 } 305 fallthrough 306 307 default: 308 // The remaining properties do not have a defined ordering, so 309 // we'll leave it unspecified. Since we use sort.Stable in 310 // the caller of this, the ordering of remaining items will 311 // be preserved. 312 return false 313 } 314 } 315 316 func (sd sortDiagnostics) Swap(i, j int) { 317 sd[i], sd[j] = sd[j], sd[i] 318 }