refactor suppress errors (#1016)

* refactor suppress errors

* better name
This commit is contained in:
Paul Wells
2025-03-20 21:35:17 -07:00
committed by GitHub
parent 6d044a0462
commit 157eda585c
2 changed files with 11 additions and 15 deletions

View File

@@ -16,10 +16,10 @@ package psrpc
import (
"context"
"errors"
"google.golang.org/protobuf/proto"
"github.com/livekit/protocol/utils"
"github.com/livekit/psrpc"
)
@@ -36,22 +36,11 @@ func WithSuppressServerErrors(errs ...error) psrpc.ServerOption {
)
}
func suppressErrors(err error, ignored ...error) error {
if err != nil {
for _, e := range ignored {
if errors.Is(err, e) {
return nil
}
}
}
return err
}
func newClientRPCErrorInterceptor(errs ...error) psrpc.ClientRPCInterceptor {
return func(rpcInfo psrpc.RPCInfo, next psrpc.ClientRPCHandler) psrpc.ClientRPCHandler {
return func(ctx context.Context, req proto.Message, opts ...psrpc.RequestOption) (res proto.Message, err error) {
res, err = next(ctx, req, opts...)
return res, suppressErrors(err, errs...)
return res, utils.ScreenError(err, errs...)
}
}
}
@@ -59,7 +48,7 @@ func newClientRPCErrorInterceptor(errs ...error) psrpc.ClientRPCInterceptor {
func newServerRPCErorrInterceptor(errs ...error) psrpc.ServerRPCInterceptor {
return func(ctx context.Context, req proto.Message, rpcInfo psrpc.RPCInfo, handler psrpc.ServerRPCHandler) (res proto.Message, err error) {
res, err = handler(ctx, req)
return res, suppressErrors(err, errs...)
return res, utils.ScreenError(err, errs...)
}
}
@@ -78,5 +67,5 @@ type multiRPCErorrInterceptor struct {
}
func (r *multiRPCErorrInterceptor) Recv(msg proto.Message, err error) {
r.ClientMultiRPCHandler.Recv(msg, suppressErrors(err, r.errors...))
r.ClientMultiRPCHandler.Recv(msg, utils.ScreenError(err, r.errors...))
}

View File

@@ -10,3 +10,10 @@ func ErrorIsOneOf(err error, targets ...error) bool {
}
return false
}
func ScreenError(err error, ignored ...error) error {
if ErrorIsOneOf(err, ignored...) {
return nil
}
return err
}