From 6fe0bd55a84fe7c00b575a07b3f41ca04ea72c5b Mon Sep 17 00:00:00 2001 From: Jan Seidl Date: Fri, 10 Nov 2017 13:51:56 +0100 Subject: [PATCH] * travis build 1.6+ * Error type implements error and (hashicorp) errwrap interface --- .travis.yml | 1 - Makefile | 3 --- README.md | 25 +++++++++++++++++++++++++ VERSION | 1 + retry.go | 29 ++++++++++++++++++++++------- retry_test.go | 2 +- 6 files changed, 49 insertions(+), 12 deletions(-) create mode 100644 VERSION diff --git a/.travis.yml b/.travis.yml index 9636ed9..54003a4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: go go: - - 1.5 - 1.6 - 1.7 - 1.8 diff --git a/Makefile b/Makefile index 18ccfb7..d36978e 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,6 @@ TEST_PATTERN?=. TEST_OPTIONS?= DEP?=$$(which dep) -GO15VENDOREXPERIMENT=1 -export GO15VENDOREXPERIMENT - ifeq ($(OS),Windows_NT) DEP_VERS=dep-windows-amd64 else diff --git a/README.md b/README.md index 179462d..9389257 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,31 @@ func RetryWithOpts(retryableFunction Retryable, opts RetryOpts) error ``` RetryWithOpts - customizable retry via RetryOpts +#### type Error + +```go +type Error []error +``` + +Error type represents list of errors in retry + +#### func (Error) Error + +```go +func (e Error) Error() string +``` +Error method return string representation of Error It is an implementation of +error interface + +#### func (Error) WrappedErrors + +```go +func (e Error) WrappedErrors() []error +``` +WrappedErrors returns the list of errors that this Error is wrapping. It is an +implementation of the errwrap.Wrapper interface so that multierror.Error can be +used with that library. + #### type OnRetry ```go diff --git a/VERSION b/VERSION new file mode 100644 index 0000000..0d91a54 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.3.0 diff --git a/retry.go b/retry.go index 77d68e3..25664c1 100644 --- a/retry.go +++ b/retry.go @@ -39,6 +39,10 @@ SEE ALSO * [cenkalti/backoff](https://github.com/cenkalti/backoff) - Go port of the exponential backoff algorithm from Google's HTTP Client Library for Java. Really complicated interface. +* [rafaeljesus/retry-go](https://github.com/rafaeljesus/retry-go) - looks good, slightly similar as this package, don't have 'simple' `Retry` method + +* [matryer/try](https://github.com/matryer/try) - very popular package, nonintuitive interface (for me) + */ package retry @@ -71,7 +75,7 @@ func RetryWithOpts(retryableFunction Retryable, opts RetryOpts) error { func RetryCustom(retryableFunction Retryable, onRetryFunction OnRetry, opts RetryOpts) error { var n uint - errorLog := make(errorLog, opts.tries) + errorLog := make(Error, opts.tries) for n < opts.tries { err := retryableFunction() @@ -89,16 +93,27 @@ func RetryCustom(retryableFunction Retryable, onRetryFunction OnRetry, opts Retr n++ } - return fmt.Errorf("All (%d) retries fail:\n%s", opts.tries, errorLog) + return errorLog } -type errorLog []error +// Error type represents list of errors in retry +type Error []error -func (log errorLog) String() string { - logWithNumber := make([]string, len(log)) - for i, l := range log { +// Error method return string representation of Error +// It is an implementation of error interface +func (e Error) Error() string { + logWithNumber := make([]string, len(e)) + for i, l := range e { logWithNumber[i] = fmt.Sprintf("#%d: %s", i+1, l.Error()) } - return strings.Join(logWithNumber, "\n") + return fmt.Sprintf("All retries fail:\n%s", strings.Join(logWithNumber, "\n")) +} + +// WrappedErrors returns the list of errors that this Error is wrapping. +// It is an implementation of the `errwrap.Wrapper` interface +// in package [errwrap](https://github.com/hashicorp/errwrap) so that +// `retry.Error` can be used with that library. +func (e Error) WrappedErrors() []error { + return e } diff --git a/retry_test.go b/retry_test.go index cabde3e..e8b6ca6 100644 --- a/retry_test.go +++ b/retry_test.go @@ -17,7 +17,7 @@ func TestCustom(t *testing.T) { ) assert.Error(t, err) - expectedErrorFormat := `All (10) retries fail: + expectedErrorFormat := `All retries fail: #1: test #2: test #3: test