Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper/resource: Require explicit usage of NonRetryableError and RetryableError #199

Merged
merged 1 commit into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions helper/resource/wait.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package resource

import (
"errors"
"sync"
"time"
)
Expand Down Expand Up @@ -66,19 +67,31 @@ type RetryError struct {
}

// RetryableError is a helper to create a RetryError that's retryable from a
// given error.
// given error. To prevent logic errors, will return an error when passed a
// nil error.
func RetryableError(err error) *RetryError {
if err == nil {
return nil
return &RetryError{
Err: errors.New("empty retryable error received. " +
"This is a bug with the Terraform provider and should be " +
"reported as a GitHub issue in the provider repository."),
Retryable: false,
}
}
return &RetryError{Err: err, Retryable: true}
}

// NonRetryableError is a helper to create a RetryError that's _not_ retryable
// from a given error.
// from a given error. To prevent logic errors, will return an error when
// passed a nil error.
func NonRetryableError(err error) *RetryError {
if err == nil {
return nil
return &RetryError{
Err: errors.New("empty non-retryable error received. " +
"This is a bug with the Terraform provider and should be " +
"reported as a GitHub issue in the provider repository."),
Retryable: false,
}
}
return &RetryError{Err: err, Retryable: false}
}
26 changes: 26 additions & 0 deletions helper/resource/wait_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,29 @@ func TestRetry_error(t *testing.T) {
t.Fatal("timeout")
}
}

func TestRetry_nilNonRetryableError(t *testing.T) {
t.Parallel()

f := func() *RetryError {
return NonRetryableError(nil)
}

err := Retry(1*time.Second, f)
if err == nil {
t.Fatal("should error")
}
}

func TestRetry_nilRetryableError(t *testing.T) {
t.Parallel()

f := func() *RetryError {
return RetryableError(nil)
}

err := Retry(1*time.Second, f)
if err == nil {
t.Fatal("should error")
}
}