refactor suppress errors (#1016)
* refactor suppress errors * better name
This commit is contained in:
@@ -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...))
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user