Skip to content

Commit

Permalink
chore: integrate review suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
42atomys committed Sep 4, 2024
1 parent 8521819 commit 1a00d0e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,5 @@ func TestDefaultHandler_safeWrapper(t *testing.T) {
func TestSafeFuncName(t *testing.T) {
assert.Equal(t, "safeFn", safeFuncName("fn"))
assert.Equal(t, "safeFn", safeFuncName("Fn"))
assert.Equal(t, "", safeFuncName(""))
assert.Empty(t, safeFuncName(""))
}
4 changes: 3 additions & 1 deletion internal/runtime/safecall.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"reflect"
)

var ErrPanicRecovery = errors.New("recovered from a panic")

// SafeCall safely calls a function using reflection. It handles potential
// panics by recovering and returning an error. The function `fn` is expected
// to be a function, and `args` are the arguments to pass to that function.
Expand All @@ -22,7 +24,7 @@ func SafeCall(fn any, args ...any) (result any, err error) {
// Defer a function to handle panics
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("recovered from panic: %v", r)
err = fmt.Errorf("%w: %v", ErrPanicRecovery, r)
}
}()

Expand Down
3 changes: 2 additions & 1 deletion internal/runtime/safecall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ func TestSafeCall(t *testing.T) {
// Test a case where the function panics.
fn7 := func() { panic("oh no") }
out, err = SafeCall(fn7)
require.ErrorContains(t, err, "recovered from panic: oh no")
require.ErrorContains(t, err, "oh no")
require.ErrorIs(t, err, ErrPanicRecovery)
assert.Nil(t, out)

// Test when fn is not a function.
Expand Down

0 comments on commit 1a00d0e

Please sign in to comment.