Skip to content

Commit 350203c

Browse files
committed
fix: fix error is not nil if it is a nil pointer
Signed-off-by: Chen Su <ghosind@gmail.com>
1 parent c9f132d commit 350203c

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

all_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import (
1111
"github.com/ghosind/go-async"
1212
)
1313

14+
type testError struct{}
15+
16+
func (e *testError) Error() string {
17+
return "test error"
18+
}
19+
1420
func TestAllWithoutFuncs(t *testing.T) {
1521
a := assert.New(t)
1622

@@ -101,6 +107,18 @@ func TestAllWithTimeoutContext(t *testing.T) {
101107
a.EqualNow(out, [][]any{{nil}, {nil}, nil, nil, nil})
102108
}
103109

110+
func TestAllWithTestError(t *testing.T) {
111+
a := assert.New(t)
112+
113+
_, err := async.All(func() error {
114+
var e *testError
115+
return e
116+
}, func() *testError {
117+
return nil
118+
})
119+
a.NilNow(err)
120+
}
121+
104122
func BenchmarkAll(b *testing.B) {
105123
tasks := make([]async.AsyncFn, 0, 1000)
106124
for i := 0; i < 1000; i++ {

async.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ func invokeAsyncFn(fn AsyncFn, ctx context.Context, params []any) ([]any, error)
142142
err = nil
143143
} else {
144144
err = out[numRet-1].Interface().(error)
145+
// double check if the error is a custom error pointer
146+
if err == nil || reflect.ValueOf(err).IsNil() {
147+
err = nil
148+
}
145149
}
146150
}
147151
for i := 0; i < numRet; i++ {

async_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,4 @@ func TestInvokeAsyncFnWithParams(t *testing.T) {
230230
a.PanicOfNow(func() {
231231
invokeAsyncFn(func(ctx context.Context, n int) {}, ctx, []any{"hello"})
232232
}, ErrUnmatchedParam)
233-
234233
}

0 commit comments

Comments
 (0)