-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathhelpers.go
42 lines (38 loc) · 1019 Bytes
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
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.
//
// if err != nil {
// return diag.FromErr(err)
// }
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.
//
// if unexpectedCondition {
// return diag.Errorf("unexpected: %s", someValue)
// }
func Errorf(format string, a ...interface{}) Diagnostics {
return Diagnostics{
Diagnostic{
Severity: Error,
Summary: fmt.Sprintf(format, a...),
},
}
}