Skip to content

Commit

Permalink
chore: increase robustness of safe call
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Sep 5, 2024
1 parent bcfa9e2 commit b5d4858
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
37 changes: 17 additions & 20 deletions internal/runtime/safecall.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
)

var (
ErrPanicRecovery = errors.New("recovered from a panic")
ErrInvalidFunction = errors.New("fn is not a function")
ErrIncorrectArguments = errors.New("incorrect number of arguments")
ErrPanicRecovery = errors.New("cannot safecall function: recovered from a panic")
ErrInvalidFunction = errors.New("cannot safecall function: first argument is not a function")
ErrIncorrectArguments = errors.New("cannot safecall function: number of arguments does not match function's input arity")
ErrMoreThanTwoReturns = errors.New("cannot safecall function: function returns more than two values")
ErrInvalidLastReturnType = errors.New("cannot safecall function: invalid last return type (expected error)")
)

// SafeCall safely calls a function using reflection. It handles potential
Expand Down Expand Up @@ -58,23 +60,18 @@ func SafeCall(fn any, args ...any) (result any, err error) {
// Call the function using reflection
out := fnValue.Call(in)

// Process the output
if len(out) == 0 {
switch len(out) {
case 0:
return nil, nil
case 1:
return out[0].Interface(), nil
case 2:
if out[1].Type().Implements(reflect.TypeOf((*error)(nil)).Elem()) {
err, _ = out[1].Interface().(error)
return out[0].Interface(), err
}
return out[0].Interface(), ErrInvalidLastReturnType
default:
return out[0].Interface(), ErrMoreThanTwoReturns
}

// If there's only one return value
result = out[0].Interface()
if len(out) == 1 {
return result, nil
}

// If there are two return values (assuming the second is an error)
if len(out) == 2 && out[1].Type().Implements(reflect.TypeOf((*error)(nil)).Elem()) {
err, _ = out[1].Interface().(error)
return result, err
}

// Handle other cases as needed
return result, nil
}
31 changes: 28 additions & 3 deletions internal/runtime/safecall_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package runtime

import (
"errors"
"fmt"
"testing"

Expand Down Expand Up @@ -43,8 +44,8 @@ func TestSafeCall(t *testing.T) {
a, b, c := "a", "b", "c"
fn6 := func(a, b, c string) (string, string, string) { return a, b, c }
out, err = SafeCall(fn6, a, b, c)
require.NoError(t, err)
assert.Equal(t, out, a, "the return should be the first argument")
require.ErrorIs(t, err, ErrMoreThanTwoReturns)
assert.Equal(t, out, a)

// Test a case where the function panics.
fn7 := func() { panic("oh no") }
Expand All @@ -56,7 +57,7 @@ func TestSafeCall(t *testing.T) {
// Test when fn is not a function.
fn8 := "cheese"
out, err = SafeCall(fn8)
require.ErrorContains(t, err, "fn is not a function")
require.ErrorIs(t, err, ErrInvalidFunction)
assert.Nil(t, out)

// Test a function taking nil arguments
Expand All @@ -70,4 +71,28 @@ func TestSafeCall(t *testing.T) {
out, err = SafeCall(fn10, nil, nil, nil)
require.NoError(t, err)
assert.Equal(t, "crap", out)

// Test to send a function with more than 2 returns
fn13 := func() (string, string, error) { return "a", "b", errors.New("c") }
out, err = SafeCall(fn13)
require.ErrorContains(t, err, "c")
assert.Equal(t, "a", out)

// Test to send more arguments than the function expects
fn11 := func(a, b any) string { return "crap" }
out, err = SafeCall(fn11, "a", "b", "c")
require.ErrorIs(t, err, ErrIncorrectArguments)
assert.Nil(t, out)

// Test to send a function with more than 2 returns and the second return is not an error
fn14 := func() (string, string, string) { return "a", "b", "c" }
out, err = SafeCall(fn14)
require.ErrorIs(t, err, ErrMoreThanTwoReturns)
assert.Equal(t, "a", out)

// Test to send a function with 2 returns and the second return is not an error
fn15 := func() (string, string) { return "a", "b" }
out, err = SafeCall(fn15)
require.ErrorIs(t, err, ErrInvalidLastReturnType)
assert.Equal(t, "a", out)
}

0 comments on commit b5d4858

Please sign in to comment.