-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathdiag.go
63 lines (53 loc) · 1.32 KB
/
diag.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package errs
import (
"fmt"
"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
)
const (
summaryInvalidValue = "Invalid value"
summaryInvalidValueType = "Invalid value type"
)
func NewIncorrectValueTypeAttributeError(path cty.Path, expected string) diag.Diagnostic {
return NewAttributeErrorDiagnostic(
path,
summaryInvalidValueType,
fmt.Sprintf("Expected type to be %s", expected),
)
}
func NewInvalidValueAttributeErrorf(path cty.Path, format string, a ...any) diag.Diagnostic {
return NewInvalidValueAttributeError(
path,
fmt.Sprintf(format, a...),
)
}
func NewInvalidValueAttributeError(path cty.Path, detail string) diag.Diagnostic {
return NewAttributeErrorDiagnostic(
path,
summaryInvalidValue,
detail,
)
}
func NewAttributeErrorDiagnostic(path cty.Path, summary, detail string) diag.Diagnostic {
return withPath(
NewErrorDiagnostic(summary, detail),
path,
)
}
func NewErrorDiagnostic(summary, detail string) diag.Diagnostic {
return diag.Diagnostic{
Severity: diag.Error,
Summary: summary,
Detail: detail,
}
}
func FromAttributeError(path cty.Path, err error) diag.Diagnostic {
return withPath(
NewErrorDiagnostic(err.Error(), ""),
path,
)
}
func withPath(d diag.Diagnostic, path cty.Path) diag.Diagnostic {
d.AttributePath = path
return d
}