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

Proposal: expose MultiError #8

Merged
merged 1 commit into from
Oct 3, 2024
Merged
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
17 changes: 10 additions & 7 deletions semgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Group struct {
wg sync.WaitGroup
ctx context.Context

errs multiError
errs MultiError
mu sync.Mutex // protects errs
}

Expand Down Expand Up @@ -70,15 +70,17 @@ func (g *Group) Go(f func() error) {
}

// Wait blocks until all function calls from the Go method have returned, then
// returns all accumulated non-nil error (if any) from them.
// returns all accumulated non-nil errors (if any) from them.
//
// If a non-nil error is returned, it will be of type [MultiError].
func (g *Group) Wait() error {
g.wg.Wait()
return g.errs.ErrorOrNil()
}

type multiError []error
type MultiError []error

func (e multiError) Error() string {
func (e MultiError) Error() string {
var b strings.Builder
fmt.Fprintf(&b, "%d error(s) occurred:\n", len(e))

Expand All @@ -92,15 +94,16 @@ func (e multiError) Error() string {
return b.String()
}

func (e multiError) ErrorOrNil() error {
// ErrorOrNil returns nil if there are no errors, otherwise returns itself.
func (e MultiError) ErrorOrNil() error {
if len(e) == 0 {
return nil
}

return e
}

func (e multiError) Is(target error) bool {
func (e MultiError) Is(target error) bool {
for _, err := range e {
if errors.Is(err, target) {
return true
Expand All @@ -109,7 +112,7 @@ func (e multiError) Is(target error) bool {
return false
}

func (e multiError) As(target interface{}) bool {
func (e MultiError) As(target interface{}) bool {
for _, err := range e {
if errors.As(err, target) {
return true
Expand Down
12 changes: 12 additions & 0 deletions semgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func TestGroup_multiple_tasks_errors(t *testing.T) {
if err == nil {
t.Fatalf("g.Wait() should return an error")
}
if !errors.As(err, &MultiError{}) {
t.Fatalf("the error should be of type MultiError")
}

wantErr := `2 error(s) occurred:
* foo
Expand Down Expand Up @@ -124,6 +127,15 @@ func TestGroup_multiple_tasks_errors_Is(t *testing.T) {
if errors.Is(err, bazErr) {
t.Errorf("error should not be contained %v\n", bazErr)
}

var gotMultiErr MultiError
if !errors.As(err, &gotMultiErr) {
t.Fatalf("error should be matched MultiError")
}
expectedErr := (MultiError{fooErr, barErr}).Error()
if gotMultiErr.Error() != expectedErr {
t.Errorf("error should be %q, got %q", expectedErr, gotMultiErr.Error())
}
}

type foobarErr struct{ str string }
Expand Down
Loading