Skip to content

Commit

Permalink
* travis build 1.6+
Browse files Browse the repository at this point in the history
* Error type implements error and (hashicorp) errwrap interface
  • Loading branch information
Jan Seidl committed Nov 10, 2017
1 parent b0f7555 commit 6fe0bd5
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 12 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
language: go

go:
- 1.5
- 1.6
- 1.7
- 1.8
Expand Down
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.3.0
29 changes: 22 additions & 7 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6fe0bd5

Please sign in to comment.