Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #5. Supports methods Cause and Unwrap #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
}