github.com/livekit/protocol@v1.39.3/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  
    20  	"google.golang.org/protobuf/proto"
    21  
    22  	"github.com/livekit/protocol/utils"
    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 newClientRPCErrorInterceptor(errs ...error) psrpc.ClientRPCInterceptor {
    40  	return func(rpcInfo psrpc.RPCInfo, next psrpc.ClientRPCHandler) psrpc.ClientRPCHandler {
    41  		return func(ctx context.Context, req proto.Message, opts ...psrpc.RequestOption) (res proto.Message, err error) {
    42  			res, err = next(ctx, req, opts...)
    43  			return res, utils.ScreenError(err, errs...)
    44  		}
    45  	}
    46  }
    47  
    48  func newServerRPCErorrInterceptor(errs ...error) psrpc.ServerRPCInterceptor {
    49  	return func(ctx context.Context, req proto.Message, rpcInfo psrpc.RPCInfo, handler psrpc.ServerRPCHandler) (res proto.Message, err error) {
    50  		res, err = handler(ctx, req)
    51  		return res, utils.ScreenError(err, errs...)
    52  	}
    53  }
    54  
    55  func newMultiRPCErorrInterceptor(errs ...error) psrpc.ClientMultiRPCInterceptor {
    56  	return func(rpcInfo psrpc.RPCInfo, next psrpc.ClientMultiRPCHandler) psrpc.ClientMultiRPCHandler {
    57  		return &multiRPCErorrInterceptor{
    58  			ClientMultiRPCHandler: next,
    59  			errors:                errs,
    60  		}
    61  	}
    62  }
    63  
    64  type multiRPCErorrInterceptor struct {
    65  	psrpc.ClientMultiRPCHandler
    66  	errors []error
    67  }
    68  
    69  func (r *multiRPCErorrInterceptor) Recv(msg proto.Message, err error) {
    70  	r.ClientMultiRPCHandler.Recv(msg, utils.ScreenError(err, r.errors...))
    71  }