Skip to content

Commit

Permalink
Fix gojek#5. Supports methods Cause and Unwrap for correct working er…
Browse files Browse the repository at this point in the history
…rors.Is() in go 1.13
  • Loading branch information
Vladimir Artamonov committed May 27, 2020
1 parent 8f62c1e commit 3252db4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Valkyrie - Changelog

## v0.1.0 (2020-MAY-27)
- supports method Cause
- supports method Unwrap for Go 1.13 error chains

## v0.0.1 (2018-JAN-19)

- initial fork commit
Expand Down
17 changes: 17 additions & 0 deletions multierror.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ func (m *MultiError) Error() string {

return strings.Join(formattedError, ", ")
}

// Cause returns the first original error.
func (m *MultiError) Cause() error {
m.mutex.Lock()
defer m.mutex.Unlock()

if len(m.errs) == 0 {
return nil
}

return m.errs[0]
}

// Unwrap provides compatibility for go 1.13 error chains.
func (m *MultiError) Unwrap() error {
return m.Cause()
}
26 changes: 26 additions & 0 deletions multierror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,29 @@ func TestMultiErrorWhileConcurrentPushShouldNotPanic(t *testing.T) {
}
assert.NotPanics(t, concurrentPushAndErrors)
}

func TestMultiError_Cause(t *testing.T) {
me := &MultiError{}
assert.NoError(t, me.Cause())

me.Push("one")
me.Push("two")
me.Push("three")

err := me.Cause()
assert.Error(t, err)
assert.Equal(t, "one", err.Error())
}

func TestMultiError_Unwrap(t *testing.T) {
me := &MultiError{}
assert.NoError(t, me.Unwrap())

me.Push("one")
me.Push("two")
me.Push("three")

err := me.Unwrap()
assert.Error(t, err)
assert.Equal(t, "one", err.Error())
}

0 comments on commit 3252db4

Please sign in to comment.