diff --git a/pkg/rpc/manager.go b/pkg/rpc/manager.go index d5817b3..2a59cbd 100644 --- a/pkg/rpc/manager.go +++ b/pkg/rpc/manager.go @@ -7,6 +7,7 @@ import ( "sync" "github.com/google/uuid" + "github.com/pojntfx/dudirekta/pkg/utils" ) var ( @@ -55,7 +56,11 @@ func createClosure(fn interface{}) (func(args ...interface{}) (interface{}, erro } } - out := reflect.ValueOf(fn).Call(in) + out, err := utils.Call(reflect.ValueOf(fn), in) + if err != nil { + return nil, err + } + if len(out) == 1 { if out[0].IsValid() && !out[0].IsNil() { return nil, out[0].Interface().(error) diff --git a/pkg/rpc/registry.go b/pkg/rpc/registry.go index 20f3edd..295e0d4 100644 --- a/pkg/rpc/registry.go +++ b/pkg/rpc/registry.go @@ -10,6 +10,7 @@ import ( "time" "github.com/google/uuid" + "github.com/pojntfx/dudirekta/pkg/utils" "github.com/teivah/broadcast" ) @@ -400,7 +401,12 @@ func (r Registry[R]) Link( rpcArgs = append(rpcArgs, args[i].Interface()) } - rcpRv := rpc.Call([]reflect.Value{reflect.ValueOf(r.ctx), reflect.ValueOf(closureID), reflect.ValueOf(rpcArgs)}) + rcpRv, err := utils.Call(rpc, []reflect.Value{reflect.ValueOf(r.ctx), reflect.ValueOf(closureID), reflect.ValueOf(rpcArgs)}) + if err != nil { + errs <- err + + return + } rv := []reflect.Value{} if functionType.NumOut() == 1 { @@ -439,7 +445,12 @@ func (r Registry[R]) Link( } go func() { - res := function.Call(args) + res, err := utils.Call(function, args) + if err != nil { + errs <- err + + return + } switch len(res) { case 0: diff --git a/pkg/utils/call.go b/pkg/utils/call.go new file mode 100644 index 0000000..69d210f --- /dev/null +++ b/pkg/utils/call.go @@ -0,0 +1,26 @@ +package utils + +import ( + "errors" + "reflect" +) + +var ( + ErrPanickedWithNonErrorValue = errors.New("panicked with no error value") +) + +func Call(fn reflect.Value, in []reflect.Value) (out []reflect.Value, err error) { + defer func() { + if e := recover(); e != nil { + var ok bool + err, ok = e.(error) + if !ok { + err = ErrPanickedWithNonErrorValue + } + } + }() + + out = fn.Call(in) + + return +}