Skip to content

Commit

Permalink
Proposal: expose MultiError (#8)
Browse files Browse the repository at this point in the history
One of the great things about semgroup over x/sync/errgroup is that it
actually waits all tasks to complete and returns an error.

However, the returned error type doesn't let the caller to iterate over the
errors (e.g. my use case requires me to truncate the list of errors).

Therefore, introducing a backwards-compatible change that exports `MultiError`
type, and guarantees that `Wait()` method returns an error of this type.
  • Loading branch information
ahmetb authored Oct 3, 2024
1 parent de40458 commit 7b4f395
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
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

0 comments on commit 7b4f395

Please sign in to comment.