Skip to content

Commit

Permalink
Fix panic on nil interface conversion (#34)
Browse files Browse the repository at this point in the history
Signed-off-by: Dmytro Vasylyshyn <vasilishin.d.o@gmail.com>
  • Loading branch information
dmvass authored Jul 1, 2024
1 parent 6c533d4 commit 59bc3d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
4 changes: 2 additions & 2 deletions invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func (i *invoker) Invoke(fn any) (InvokeResult, error) {
if index == len(fnOutArgs)-1 {
// And type of the value is the error.
if fnOut.Type().Implements(errorType) {
// Use the value as an error.
result.err = fnOut.Interface().(error)
// Use the value as an error. Perform safe type conversion to avoid panics.
result.err, _ = fnOut.Interface().(error)
}
}

Expand Down
10 changes: 10 additions & 0 deletions invoker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ func TestInvokerService(t *testing.T) {
},
wantErr: false,
},
{
name: "ReturnNilError",
haveFn: func(var1 string, var2 int) error { return nil },
wantFn: func(t *testing.T, value InvokeResult) {
equal(t, len(value.Values()), 1)
equal(t, value.Values()[0], nil)
equal(t, value.Error(), nil)
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 59bc3d5

Please sign in to comment.