Skip to content

Commit

Permalink
UPSTREAM: Add and use 'internal/tfresource' package. (hashicorp#15477)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewbankkit authored and staebler committed Dec 15, 2021
1 parent b47e8da commit 045a9cf
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
21 changes: 21 additions & 0 deletions aws/internal/tfresource/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tfresource

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

// NotFound returns true if the error represents a "resource not found" condition.
// Specifically, NotFound returns true if the error is of type resource.NotFoundError.
func NotFound(err error) bool {
_, ok := err.(*resource.NotFoundError)
return ok
}

// TimedOut returns true if the error represents a "wait timed out" condition.
// Specifically, TimedOut returns true if the error matches all these conditions:
// * err is of type resource.TimeoutError
// * TimeoutError.LastError is nil
func TimedOut(err error) bool {
timeoutErr, ok := err.(*resource.TimeoutError)
return ok && timeoutErr.LastError == nil
}
97 changes: 97 additions & 0 deletions aws/internal/tfresource/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package tfresource

import (
"errors"
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestNotFound(t *testing.T) {
testCases := []struct {
Name string
Err error
Expected bool
}{
{
Name: "nil error",
Err: nil,
},
{
Name: "other error",
Err: errors.New("test"),
},
{
Name: "not found error",
Err: &resource.NotFoundError{LastError: errors.New("test")},
Expected: true,
},
{
Name: "wrapped other error",
Err: fmt.Errorf("test: %w", errors.New("test")),
},
{
Name: "wrapped not found error",
Err: fmt.Errorf("test: %w", &resource.NotFoundError{LastError: errors.New("test")}),
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
got := NotFound(testCase.Err)

if got != testCase.Expected {
t.Errorf("got %t, expected %t", got, testCase.Expected)
}
})
}
}

func TestTimedOut(t *testing.T) {
testCases := []struct {
Name string
Err error
Expected bool
}{
{
Name: "nil error",
Err: nil,
},
{
Name: "other error",
Err: errors.New("test"),
},
{
Name: "timeout error",
Err: &resource.TimeoutError{},
Expected: true,
},
{
Name: "timeout error non-nil last error",
Err: &resource.TimeoutError{LastError: errors.New("test")},
},
{
Name: "wrapped other error",
Err: fmt.Errorf("test: %w", errors.New("test")),
},
{
Name: "wrapped timeout error",
Err: fmt.Errorf("test: %w", &resource.TimeoutError{}),
},
{
Name: "wrapped timeout error non-nil last error",
Err: fmt.Errorf("test: %w", &resource.TimeoutError{LastError: errors.New("test")}),
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
got := TimedOut(testCase.Err)

if got != testCase.Expected {
t.Errorf("got %t, expected %t", got, testCase.Expected)
}
})
}
}

0 comments on commit 045a9cf

Please sign in to comment.