-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17613 from hashicorp/error-proposal
Proposal: Add more fine-grained errors for finder functions
- Loading branch information
Showing
4 changed files
with
244 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package tfresource | ||
|
||
import ( | ||
"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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package tfresource | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestEmptyResultErrorAsNotFoundError(t *testing.T) { | ||
lastRequest := 123 | ||
err := NewEmptyResultError(lastRequest) | ||
|
||
var nfe *resource.NotFoundError | ||
ok := errors.As(err, &nfe) | ||
|
||
if !ok { | ||
t.Fatal("expected errors.As() to return true") | ||
} | ||
if nfe.Message != "empty result" { | ||
t.Errorf(`expected Message to be "empty result", got %q`, nfe.Message) | ||
} | ||
if nfe.LastRequest != lastRequest { | ||
t.Errorf("unexpected value for LastRequest") | ||
} | ||
} | ||
|
||
func TestEmptyResultErrorIs(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
err error | ||
expected bool | ||
}{ | ||
{ | ||
name: "compare to nil", | ||
err: nil, | ||
}, | ||
{ | ||
name: "other error", | ||
err: errors.New("test"), | ||
}, | ||
{ | ||
name: "EmptyResultError with LastRequest", | ||
err: &EmptyResultError{ | ||
LastRequest: 123, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "ErrEmptyResult", | ||
err: ErrEmptyResult, | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped other error", | ||
err: fmt.Errorf("test: %w", errors.New("test")), | ||
}, | ||
{ | ||
name: "wrapped EmptyResultError with LastRequest", | ||
err: fmt.Errorf("test: %w", &EmptyResultError{ | ||
LastRequest: 123, | ||
}), | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped ErrEmptyResult", | ||
err: fmt.Errorf("test: %w", ErrEmptyResult), | ||
expected: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
err := &EmptyResultError{} | ||
ok := errors.Is(testCase.err, err) | ||
if ok != testCase.expected { | ||
t.Errorf("got %t, expected %t", ok, testCase.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestTooManyResultsErrorAsNotFoundError(t *testing.T) { | ||
count := 2 | ||
lastRequest := 123 | ||
err := NewTooManyResultsError(count, lastRequest) | ||
|
||
var nfe *resource.NotFoundError | ||
ok := errors.As(err, &nfe) | ||
|
||
if !ok { | ||
t.Fatal("expected errors.As() to return true") | ||
} | ||
if expected := fmt.Sprintf("too many results: wanted 1, got %d", count); nfe.Message != expected { | ||
t.Errorf(`expected Message to be %q, got %q`, expected, nfe.Message) | ||
} | ||
if nfe.LastRequest != lastRequest { | ||
t.Errorf("unexpected value for LastRequest") | ||
} | ||
} | ||
|
||
func TestTooManyResultsErrorIs(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
err error | ||
expected bool | ||
}{ | ||
{ | ||
name: "compare to nil", | ||
err: nil, | ||
}, | ||
{ | ||
name: "other error", | ||
err: errors.New("test"), | ||
}, | ||
{ | ||
name: "TooManyResultsError with LastRequest", | ||
err: &TooManyResultsError{ | ||
LastRequest: 123, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "ErrTooManyResults", | ||
err: ErrTooManyResults, | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped other error", | ||
err: fmt.Errorf("test: %w", errors.New("test")), | ||
}, | ||
{ | ||
name: "wrapped TooManyResultsError with LastRequest", | ||
err: fmt.Errorf("test: %w", &TooManyResultsError{ | ||
LastRequest: 123, | ||
}), | ||
expected: true, | ||
}, | ||
{ | ||
name: "wrapped ErrTooManyResults", | ||
err: fmt.Errorf("test: %w", ErrTooManyResults), | ||
expected: true, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
err := &TooManyResultsError{} | ||
ok := errors.Is(testCase.err, err) | ||
if ok != testCase.expected { | ||
t.Errorf("got %t, expected %t", ok, testCase.expected) | ||
} | ||
}) | ||
} | ||
} |