Skip to content

Commit 28b43da

Browse files
committed
feat: invokeAsyncFn fill out slice if the function panics.
1 parent 6eb73f5 commit 28b43da

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

async.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"reflect"
66

7-
"github.com/ghosind/utils"
7+
"github.com/ghosind/go-try"
88
)
99

1010
// AsyncFn is the function to run, the function can be a function without any restriction that accepts any parameters and any return values. For the best practice, please define the function like the following styles:
@@ -100,13 +100,15 @@ func invokeAsyncFn(fn AsyncFn, ctx context.Context, params []any) ([]any, error)
100100
in := makeFuncIn(ft, ctx, params)
101101

102102
numRet := ft.NumOut()
103-
ret := make([]any, 0, numRet)
103+
ret := make([]any, numRet)
104104

105-
err := utils.Try(func() error {
105+
_, err := try.Try(func() {
106106
out = fv.Call(in)
107-
return nil
108107
})
109108
if err != nil {
109+
for i := 0; i < numRet; i++ {
110+
ret[i] = reflect.Zero(ft.Out(i)).Interface()
111+
}
110112
return ret, err
111113
}
112114

@@ -118,7 +120,7 @@ func invokeAsyncFn(fn AsyncFn, ctx context.Context, params []any) ([]any, error)
118120
}
119121
}
120122
for i := 0; i < numRet; i++ {
121-
ret = append(ret, out[i].Interface())
123+
ret[i] = out[i].Interface()
122124
}
123125

124126
return ret, err

async_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ func TestInvokeAsyncFn(t *testing.T) {
9898
ret, err = invokeAsyncFn(func() (int, string, error) { return 1, "test", nil }, ctx, nil)
9999
a.NilNow(err)
100100
a.EqualNow(ret, []any{1, "test", nil})
101+
102+
ret, err = invokeAsyncFn(func() int {
103+
panic(expectErr)
104+
}, ctx, nil)
105+
a.EqualNow(err, expectErr)
106+
a.EqualNow(ret, []any{0})
101107
}
102108

103109
func TestInvokeAsyncFnWithParams(t *testing.T) {

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@ go 1.18
44

55
require (
66
github.com/ghosind/go-assert v1.0.3
7-
github.com/ghosind/utils v0.2.0
7+
github.com/ghosind/go-try v1.0.0
88
)
9-
10-
require golang.org/x/exp v0.0.0-20231127185646-65229373498e // indirect

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
github.com/ghosind/go-assert v1.0.3 h1:K77TgGbzCNLPr19MYm2VkYySwOmRixLUZx9Z0BY96O4=
22
github.com/ghosind/go-assert v1.0.3/go.mod h1:y1ayrGzScwcWG3TiTjJuFstq9oUHUE1Sk+DzH6CKVHY=
3-
github.com/ghosind/utils v0.2.0 h1:U7byV6dLxPY55o89DnrHKLVeOzQMIuATNOjnlyK0ko0=
4-
github.com/ghosind/utils v0.2.0/go.mod h1:TKXXia04sFjVu2qdi//Tqoo5Q87IsOeK94J7Vu+bOAk=
5-
golang.org/x/exp v0.0.0-20231127185646-65229373498e h1:Gvh4YaCaXNs6dKTlfgismwWZKyjVZXwOPfIyUaqU3No=
6-
golang.org/x/exp v0.0.0-20231127185646-65229373498e/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI=
3+
github.com/ghosind/go-try v1.0.0 h1:ek3DwNsvFWBe7lm7CwgEbEfyCNgxRPmmRk9bjleh3DM=
4+
github.com/ghosind/go-try v1.0.0/go.mod h1:M1lQctWgkFsroI1V4AeeEWzS/JX8CINL1qNfTebUNbs=

0 commit comments

Comments
 (0)