Skip to content

Commit

Permalink
Merge pull request #70 from avast/unwrap_error_support
Browse files Browse the repository at this point in the history
feat: unwrap error support (issue #65)
  • Loading branch information
JaSei authored Oct 19, 2022
2 parents 1b8d49e + 9584a17 commit 2389640
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,24 @@ jobs:
run: make setup
- name: Test
run: make ci
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
name: code-coverage-report-${{ matrix.os }}
path: coverage.txt

coverage:
needs: tests
runs-on: ubuntu-latest

steps:
- name: Download a linux coverage report
uses: actions/download-artifact@v3
with:
name: code-coverage-report-ubuntu-latest
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
env_vars: OS, GOVERSION
fail_ci_if_error: true
flags: unittest
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,25 @@ error interface
func (e Error) Is(target error) bool
```

#### func (Error) Unwrap

```go
func (e Error) Unwrap() error
```
Unwrap the last error for compatible with the `errors.Unwrap()` when you need
unwrap all erros, you should use `WrappedErrors()` instead

err := Do(
func() error {
return errors.New("original error")
},
Attempts(1),
)

fmt.Println(errors.Unwrap(err)) # "original error" is printed

added in version 4.1.0

#### func (Error) WrappedErrors

```go
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.3
4.1.0
19 changes: 19 additions & 0 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,25 @@ func (e Error) As(target interface{}) bool {
return false
}

/*
Unwrap the last error for compatible with the `errors.Unwrap()`
when you need unwrap all erros, you should use `WrappedErrors()` instead
err := Do(
func() error {
return errors.New("original error")
},
Attempts(1),
)
fmt.Println(errors.Unwrap(err)) # "original error" is printed
added in version 4.1.0
*/
func (e Error) Unwrap() error {
return e[len(e)-1]
}

func lenWithoutNil(e Error) (count int) {
for _, v := range e {
if v != nil {
Expand Down
13 changes: 13 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,16 @@ func TestErrorAs(t *testing.T) {
assert.False(t, errors.As(e, &tb))
assert.Equal(t, "foo", tf.str)
}

func TestUnwrap(t *testing.T) {
testError := errors.New("test error")
err := Do(
func() error {
return testError
},
Attempts(1),
)

assert.Error(t, err)
assert.Equal(t, testError, errors.Unwrap(err))
}

0 comments on commit 2389640

Please sign in to comment.