Skip to content

Commit

Permalink
Merge pull request #199 from hashicorp/f-helper-resource-explicit-Ret…
Browse files Browse the repository at this point in the history
…ryError

helper/resource: Require explicit usage of NonRetryableError and RetryableError
  • Loading branch information
appilon authored Feb 6, 2020
2 parents 92204d7 + 4f58f0e commit 742849e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
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")
}
}

0 comments on commit 742849e

Please sign in to comment.