github.com/livekit/protocol@v1.16.1-0.20240517185851-47e4c6bba773/psrpc/errors.go (about)

     1  // Copyright 2023 LiveKit, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package psrpc
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  
    21  	"google.golang.org/protobuf/proto"
    22  
    23  	"github.com/livekit/psrpc"
    24  )
    25  
    26  func WithSuppressClientErrors(errs ...error) psrpc.ClientOption {
    27  	return psrpc.WithClientOptions(
    28  		psrpc.WithClientRPCInterceptors(newClientRPCErrorInterceptor(errs...)),
    29  		psrpc.WithClientMultiRPCInterceptors(newMultiRPCErorrInterceptor(errs...)),
    30  	)
    31  }
    32  
    33  func WithSuppressServerErrors(errs ...error) psrpc.ServerOption {
    34  	return psrpc.WithServerOptions(
    35  		psrpc.WithServerRPCInterceptors(newServerRPCErorrInterceptor(errs...)),
    36  	)
    37  }
    38  
    39  func suppressErrors(err error, ignored ...error) error {
    40  	if err != nil {
    41  		for _, e := range ignored {
    42  			if errors.Is(err, e) {
    43  				return nil
    44  			}
    45  		}
    46  	}
    47  	return err
    48  }
    49  
    50  func newClientRPCErrorInterceptor(errs ...error) psrpc.ClientRPCInterceptor {
    51  	return func(rpcInfo psrpc.RPCInfo, next psrpc.ClientRPCHandler) psrpc.ClientRPCHandler {
    52  		return func(ctx context.Context, req proto.Message, opts ...psrpc.RequestOption) (res proto.Message, err error) {
    53  			res, err = next(ctx, req, opts...)
    54  			return res, suppressErrors(err, errs...)
    55  		}
    56  	}
    57  }
    58  
    59  func newServerRPCErorrInterceptor(errs ...error) psrpc.ServerRPCInterceptor {
    60  	return func(ctx context.Context, req proto.Message, rpcInfo psrpc.RPCInfo, handler psrpc.ServerRPCHandler) (res proto.Message, err error) {
    61  		res, err = handler(ctx, req)
    62  		return res, suppressErrors(err, errs...)
    63  	}
    64  }
    65  
    66  func newMultiRPCErorrInterceptor(errs ...error) psrpc.ClientMultiRPCInterceptor {
    67  	return func(rpcInfo psrpc.RPCInfo, next psrpc.ClientMultiRPCHandler) psrpc.ClientMultiRPCHandler {
    68  		return &multiRPCErorrInterceptor{
    69  			ClientMultiRPCHandler: next,
    70  			errors:                errs,
    71  		}
    72  	}
    73  }
    74  
    75  type multiRPCErorrInterceptor struct {
    76  	psrpc.ClientMultiRPCHandler
    77  	errors []error
    78  }
    79  
    80  func (r *multiRPCErorrInterceptor) Recv(msg proto.Message, err error) {
    81  	r.ClientMultiRPCHandler.Recv(msg, suppressErrors(err, r.errors...))
    82  }