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

feat: support Unwrap in BatchError #3639

Open
wants to merge 3 commits 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
9 changes: 8 additions & 1 deletion core/errorx/batcherror.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package errorx

import "bytes"
import (
"bytes"
"errors"
)

type (
// A BatchError is an error that can hold multiple errors.
Expand Down Expand Up @@ -50,3 +53,7 @@

return buf.String()
}

func (ea errorArray) Unwrap() error {
return errors.Join(ea...)

Check failure on line 58 in core/errorx/batcherror.go

View workflow job for this annotation

GitHub Actions / Windows

undefined: errors.Join

Check failure on line 58 in core/errorx/batcherror.go

View workflow job for this annotation

GitHub Actions / Linux

undefined: errors.Join
}
29 changes: 29 additions & 0 deletions core/errorx/batcherror_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,32 @@ func TestBatchErrorWithErrors(t *testing.T) {
assert.Equal(t, fmt.Sprintf("%s\n%s", err1, err2), batch.Err().Error())
assert.True(t, batch.NotNil())
}

func TestBatchError_Unwrap(t *testing.T) {
t.Run("nil", func(t *testing.T) {
var be BatchError
assert.Nil(t, be.Err())
assert.True(t, errors.Is(be.Err(), nil))
})

t.Run("one error", func(t *testing.T) {
var errFoo = errors.New("foo")
var errBar = errors.New("bar")
var be BatchError
be.Add(errFoo)
assert.True(t, errors.Is(be.Err(), errFoo))
assert.False(t, errors.Is(be.Err(), errBar))
})

t.Run("two errors", func(t *testing.T) {
var errFoo = errors.New("foo")
var errBar = errors.New("bar")
var errBaz = errors.New("baz")
var be BatchError
be.Add(errFoo)
be.Add(errBar)
assert.True(t, errors.Is(be.Err(), errFoo))
assert.True(t, errors.Is(be.Err(), errBar))
assert.False(t, errors.Is(be.Err(), errBaz))
})
}
Loading