github.com/argoproj/argo-cd@v1.8.7/util/grpc/errors.go (about)

     1  package grpc
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  	"google.golang.org/grpc"
     6  	"google.golang.org/grpc/codes"
     7  	"google.golang.org/grpc/status"
     8  	apierr "k8s.io/apimachinery/pkg/api/errors"
     9  )
    10  
    11  func kubeErrToGRPC(err error) error {
    12  	/*
    13  		Unmapped source Kubernetes API errors as of 2018-04-16:
    14  		* IsConflict => 409
    15  		* IsGone => 410
    16  		* IsResourceExpired => 410
    17  		* IsServerTimeout => 500
    18  		* IsTooManyRequests => 429
    19  		* IsUnexpectedServerError => should probably be a panic
    20  		* IsUnexpectedObjectError => should probably be a panic
    21  
    22  		Unmapped target gRPC codes as of 2018-04-16:
    23  		* Canceled Code = 1
    24  		* Unknown Code = 2
    25  		* ResourceExhausted Code = 8
    26  		* Aborted Code = 10
    27  		* OutOfRange Code = 11
    28  		* DataLoss Code = 15
    29  	*/
    30  
    31  	rewrapError := func(err error, code codes.Code) error {
    32  		return status.Errorf(code, err.Error())
    33  	}
    34  
    35  	switch {
    36  	case apierr.IsNotFound(err):
    37  		err = rewrapError(err, codes.NotFound)
    38  	case apierr.IsAlreadyExists(err):
    39  		err = rewrapError(err, codes.AlreadyExists)
    40  	case apierr.IsInvalid(err):
    41  		err = rewrapError(err, codes.InvalidArgument)
    42  	case apierr.IsMethodNotSupported(err):
    43  		err = rewrapError(err, codes.Unimplemented)
    44  	case apierr.IsServiceUnavailable(err):
    45  		err = rewrapError(err, codes.Unavailable)
    46  	case apierr.IsBadRequest(err):
    47  		err = rewrapError(err, codes.FailedPrecondition)
    48  	case apierr.IsUnauthorized(err):
    49  		err = rewrapError(err, codes.Unauthenticated)
    50  	case apierr.IsForbidden(err):
    51  		err = rewrapError(err, codes.PermissionDenied)
    52  	case apierr.IsTimeout(err):
    53  		err = rewrapError(err, codes.DeadlineExceeded)
    54  	case apierr.IsInternalError(err):
    55  		err = rewrapError(err, codes.Internal)
    56  
    57  	}
    58  	return err
    59  }
    60  
    61  // ErrorCodeUnaryServerInterceptor replaces Kubernetes errors with relevant gRPC equivalents, if any.
    62  func ErrorCodeUnaryServerInterceptor() grpc.UnaryServerInterceptor {
    63  	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
    64  		resp, err = handler(ctx, req)
    65  		return resp, kubeErrToGRPC(err)
    66  	}
    67  }
    68  
    69  // ErrorCodeStreamServerInterceptor replaces Kubernetes errors with relevant gRPC equivalents, if any.
    70  func ErrorCodeStreamServerInterceptor() grpc.StreamServerInterceptor {
    71  	return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    72  		err := handler(srv, ss)
    73  		return kubeErrToGRPC(err)
    74  	}
    75  }