You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/plugin/framework/diagnostics.mdx
+98-9Lines changed: 98 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,7 +48,7 @@ working with diagnostics, using functionality from the available
48
48
49
49
### Severity
50
50
51
-
`Severity` specifies whether the diagnostic is an error or a warning.
51
+
`Severity` specifies whether the diagnostic is an error or a warning. Terraform, nor the framework, supports other severity levels. Use [logging](/terraform/plugin/log/writing) for debugging or informational purposes.
52
52
53
53
- An **error** will be displayed to the practitioner and halt Terraform's
54
54
execution, not continuing to apply changes to later resources in the graph.
@@ -245,12 +245,55 @@ func (s exampleType) Validate(ctx context.Context, in tftypes.Value, path path.P
245
245
// ... further logic ...
246
246
```
247
247
248
-
#### Custom Diagnostics Types
248
+
### Consistent Diagnostic Creation
249
249
250
-
Advanced provider developers may need to further differentiate or store
is an interface type that can be implemented with these methods:
250
+
Create a helper function in your provider code using the diagnostic creation functions available in the [`diag` package](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/diag) to generate consistent diagnostics for types of errors/warnings. It is also possible to use [custom diagnostics types](#custom-diagnostics-types) to accomplish this same goal.
251
+
252
+
The [`diag` package](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/diag) provides these functions to create various diagnostics:
253
+
254
+
| Function | Description |
255
+
|---|---|
256
+
| [`diag.NewAttributeErrorDiagnostic()`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/diag#NewAttributeErrorDiagnostic) | Create a new error diagnostic with a [path](/terraform/plugin/framework/handling-data/paths). |.
257
+
| [`diag.NewAttributeWarningDiagnostic()`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/diag#NewAttributeWarningDiagnostic) | Create a new warning diagnostic with a [path](/terraform/plugin/framework/handling-data/paths). |.
258
+
| [`diag.NewErrorDiagnostic()`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/diag#NewErrorDiagnostic) | Create a new error diagnostic without a [path](/terraform/plugin/framework/handling-data/paths). |.
259
+
| [`diag.NewWarningDiagnostic()`](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/diag#NewWarningDiagnostic) | Create a new warning diagnostic without a [path](/terraform/plugin/framework/handling-data/paths). |.
260
+
261
+
In this example, the provider code is setup to always convert `error` returns from the API SDK to a consistent error diagnostic.
apiResp, err:= examplesdk.Read(/* ... */) // example API SDK call that may return an error
281
+
282
+
if err != nil {
283
+
resp.Diagnostics.Append(APIErrorDiagnostic(err))
284
+
285
+
return
286
+
}
287
+
288
+
// ... further logic ...
289
+
}
290
+
```
291
+
292
+
## Custom Diagnostics Types
293
+
294
+
Advanced provider developers may want to store additional data in diagnostics for other logic or create custom diagnostics that include specialized logic.
295
+
296
+
The [`diag.Diagnostic` interface](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/diag#Diagnostic) that can be implemented with these methods:
can be implemented with the additional `Path()` method:
307
+
To also include attribute path information, the [`diag.DiagnosticWithPath` interface](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/diag#DiagnosticWithPath) can be implemented with the additional `Path()` method:
267
308
268
309
```go
269
310
type DiagnosticWithPath interface {
270
311
Diagnostic
271
312
Path() path.Path
272
313
}
273
314
```
315
+
316
+
In this example, a custom diagnostic type stores an underlying `error` that caused the diagnostic:
317
+
318
+
```go
319
+
// UnderlyingErrorDiagnostic is an error diagnostic
0 commit comments