/
not_found_error.go
93 lines (72 loc) · 1.96 KB
/
not_found_error.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package tfresource
import (
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
type EmptyResultError struct {
LastRequest interface{}
}
var ErrEmptyResult = &EmptyResultError{}
func NewEmptyResultError(lastRequest interface{}) error {
return &EmptyResultError{
LastRequest: lastRequest,
}
}
func (e *EmptyResultError) Error() string {
return "empty result"
}
func (e *EmptyResultError) Is(err error) bool {
_, ok := err.(*EmptyResultError)
return ok
}
func (e *EmptyResultError) As(target interface{}) bool {
t, ok := target.(**resource.NotFoundError)
if !ok {
return false
}
*t = &resource.NotFoundError{
Message: e.Error(),
LastRequest: e.LastRequest,
}
return true
}
type TooManyResultsError struct {
Count int
LastRequest interface{}
}
var ErrTooManyResults = &TooManyResultsError{}
func NewTooManyResultsError(count int, lastRequest interface{}) error {
return &TooManyResultsError{
Count: count,
LastRequest: lastRequest,
}
}
func (e *TooManyResultsError) Error() string {
return fmt.Sprintf("too many results: wanted 1, got %d", e.Count)
}
func (e *TooManyResultsError) Is(err error) bool {
_, ok := err.(*TooManyResultsError)
return ok
}
func (e *TooManyResultsError) As(target interface{}) bool {
t, ok := target.(**resource.NotFoundError)
if !ok {
return false
}
*t = &resource.NotFoundError{
Message: e.Error(),
LastRequest: e.LastRequest,
}
return true
}
// SingularDataSourceFindError returns a standard error message for a singular data source's non-nil resource find error.
func SingularDataSourceFindError(resourceType string, err error) error {
if NotFound(err) {
if errors.Is(err, &TooManyResultsError{}) {
return fmt.Errorf("multiple %[1]ss matched; use additional constraints to reduce matches to a single %[1]s", resourceType)
}
return fmt.Errorf("no matching %[1]s found", resourceType)
}
return fmt.Errorf("error reading %s: %w", resourceType, err)
}