Skip to content

Commit 7b4f395

Browse files
authored
Proposal: expose MultiError (#8)
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.
1 parent de40458 commit 7b4f395

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

semgroup.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type Group struct {
2525
wg sync.WaitGroup
2626
ctx context.Context
2727

28-
errs multiError
28+
errs MultiError
2929
mu sync.Mutex // protects errs
3030
}
3131

@@ -70,15 +70,17 @@ func (g *Group) Go(f func() error) {
7070
}
7171

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

79-
type multiError []error
81+
type MultiError []error
8082

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

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

95-
func (e multiError) ErrorOrNil() error {
97+
// ErrorOrNil returns nil if there are no errors, otherwise returns itself.
98+
func (e MultiError) ErrorOrNil() error {
9699
if len(e) == 0 {
97100
return nil
98101
}
99102

100103
return e
101104
}
102105

103-
func (e multiError) Is(target error) bool {
106+
func (e MultiError) Is(target error) bool {
104107
for _, err := range e {
105108
if errors.Is(err, target) {
106109
return true
@@ -109,7 +112,7 @@ func (e multiError) Is(target error) bool {
109112
return false
110113
}
111114

112-
func (e multiError) As(target interface{}) bool {
115+
func (e MultiError) As(target interface{}) bool {
113116
for _, err := range e {
114117
if errors.As(err, target) {
115118
return true

semgroup_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func TestGroup_multiple_tasks_errors(t *testing.T) {
6262
if err == nil {
6363
t.Fatalf("g.Wait() should return an error")
6464
}
65+
if !errors.As(err, &MultiError{}) {
66+
t.Fatalf("the error should be of type MultiError")
67+
}
6568

6669
wantErr := `2 error(s) occurred:
6770
* foo
@@ -124,6 +127,15 @@ func TestGroup_multiple_tasks_errors_Is(t *testing.T) {
124127
if errors.Is(err, bazErr) {
125128
t.Errorf("error should not be contained %v\n", bazErr)
126129
}
130+
131+
var gotMultiErr MultiError
132+
if !errors.As(err, &gotMultiErr) {
133+
t.Fatalf("error should be matched MultiError")
134+
}
135+
expectedErr := (MultiError{fooErr, barErr}).Error()
136+
if gotMultiErr.Error() != expectedErr {
137+
t.Errorf("error should be %q, got %q", expectedErr, gotMultiErr.Error())
138+
}
127139
}
128140

129141
type foobarErr struct{ str string }

0 commit comments

Comments
 (0)