2022-08-06 14:21:18 +00:00
|
|
|
package diag
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// FromErr will convert an error into a Diagnostics. This returns Diagnostics
|
|
|
|
// as the most common use case in Go will be handling a single error
|
|
|
|
// returned from a function.
|
|
|
|
//
|
2022-12-24 16:57:19 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return diag.FromErr(err)
|
|
|
|
// }
|
2022-08-06 14:21:18 +00:00
|
|
|
func FromErr(err error) Diagnostics {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return Diagnostics{
|
|
|
|
Diagnostic{
|
|
|
|
Severity: Error,
|
|
|
|
Summary: err.Error(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf creates a Diagnostics with a single Error level Diagnostic entry.
|
|
|
|
// The summary is populated by performing a fmt.Sprintf with the supplied
|
|
|
|
// values. This returns a single error in a Diagnostics as errors typically
|
|
|
|
// do not occur in multiples as warnings may.
|
|
|
|
//
|
2022-12-24 16:57:19 +00:00
|
|
|
// if unexpectedCondition {
|
|
|
|
// return diag.Errorf("unexpected: %s", someValue)
|
|
|
|
// }
|
2022-08-06 14:21:18 +00:00
|
|
|
func Errorf(format string, a ...interface{}) Diagnostics {
|
|
|
|
return Diagnostics{
|
|
|
|
Diagnostic{
|
|
|
|
Severity: Error,
|
|
|
|
Summary: fmt.Sprintf(format, a...),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|