Skip to content

Commit 6fe0bd5

Browse files
author
Jan Seidl
committed
* travis build 1.6+
* Error type implements error and (hashicorp) errwrap interface
1 parent b0f7555 commit 6fe0bd5

File tree

6 files changed

+49
-12
lines changed

6 files changed

+49
-12
lines changed

.travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: go
22

33
go:
4-
- 1.5
54
- 1.6
65
- 1.7
76
- 1.8

Makefile

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ TEST_PATTERN?=.
33
TEST_OPTIONS?=
44
DEP?=$$(which dep)
55

6-
GO15VENDOREXPERIMENT=1
7-
export GO15VENDOREXPERIMENT
8-
96
ifeq ($(OS),Windows_NT)
107
DEP_VERS=dep-windows-amd64
118
else

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,31 @@ func RetryWithOpts(retryableFunction Retryable, opts RetryOpts) error
7979
```
8080
RetryWithOpts - customizable retry via RetryOpts
8181

82+
#### type Error
83+
84+
```go
85+
type Error []error
86+
```
87+
88+
Error type represents list of errors in retry
89+
90+
#### func (Error) Error
91+
92+
```go
93+
func (e Error) Error() string
94+
```
95+
Error method return string representation of Error It is an implementation of
96+
error interface
97+
98+
#### func (Error) WrappedErrors
99+
100+
```go
101+
func (e Error) WrappedErrors() []error
102+
```
103+
WrappedErrors returns the list of errors that this Error is wrapping. It is an
104+
implementation of the errwrap.Wrapper interface so that multierror.Error can be
105+
used with that library.
106+
82107
#### type OnRetry
83108

84109
```go

VERSION

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.3.0

retry.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ SEE ALSO
3939
4040
* [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.
4141
42+
* [rafaeljesus/retry-go](https://github.com/rafaeljesus/retry-go) - looks good, slightly similar as this package, don't have 'simple' `Retry` method
43+
44+
* [matryer/try](https://github.com/matryer/try) - very popular package, nonintuitive interface (for me)
45+
4246
*/
4347
package retry
4448

@@ -71,7 +75,7 @@ func RetryWithOpts(retryableFunction Retryable, opts RetryOpts) error {
7175
func RetryCustom(retryableFunction Retryable, onRetryFunction OnRetry, opts RetryOpts) error {
7276
var n uint
7377

74-
errorLog := make(errorLog, opts.tries)
78+
errorLog := make(Error, opts.tries)
7579

7680
for n < opts.tries {
7781
err := retryableFunction()
@@ -89,16 +93,27 @@ func RetryCustom(retryableFunction Retryable, onRetryFunction OnRetry, opts Retr
8993
n++
9094
}
9195

92-
return fmt.Errorf("All (%d) retries fail:\n%s", opts.tries, errorLog)
96+
return errorLog
9397
}
9498

95-
type errorLog []error
99+
// Error type represents list of errors in retry
100+
type Error []error
96101

97-
func (log errorLog) String() string {
98-
logWithNumber := make([]string, len(log))
99-
for i, l := range log {
102+
// Error method return string representation of Error
103+
// It is an implementation of error interface
104+
func (e Error) Error() string {
105+
logWithNumber := make([]string, len(e))
106+
for i, l := range e {
100107
logWithNumber[i] = fmt.Sprintf("#%d: %s", i+1, l.Error())
101108
}
102109

103-
return strings.Join(logWithNumber, "\n")
110+
return fmt.Sprintf("All retries fail:\n%s", strings.Join(logWithNumber, "\n"))
111+
}
112+
113+
// WrappedErrors returns the list of errors that this Error is wrapping.
114+
// It is an implementation of the `errwrap.Wrapper` interface
115+
// in package [errwrap](https://github.com/hashicorp/errwrap) so that
116+
// `retry.Error` can be used with that library.
117+
func (e Error) WrappedErrors() []error {
118+
return e
104119
}

retry_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestCustom(t *testing.T) {
1717
)
1818
assert.Error(t, err)
1919

20-
expectedErrorFormat := `All (10) retries fail:
20+
expectedErrorFormat := `All retries fail:
2121
#1: test
2222
#2: test
2323
#3: test

0 commit comments

Comments
 (0)