github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/shim/utils/errors.go (about)

     1  // Copyright 2021 The gVisor Authors.
     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  //     https://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 utils
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"fmt"
    21  
    22  	"github.com/containerd/containerd/errdefs"
    23  	"google.golang.org/grpc/codes"
    24  	"google.golang.org/grpc/status"
    25  )
    26  
    27  // ErrToGRPC wraps containerd's ToGRPC error mapper which depends on
    28  // github.com/pkg/errors to work correctly. Once we upgrade to containerd v1.4,
    29  // this function can go away and we can use errdefs.ToGRPC directly instead.
    30  //
    31  // TODO(github.com/SagerNet/issue/6232): Remove after upgrading to containerd v1.4
    32  func ErrToGRPC(err error) error {
    33  	return errToGRPCMsg(err, err.Error())
    34  }
    35  
    36  // ErrToGRPCf maps the error to grpc error codes, assembling the formatting
    37  // string and combining it with the target error string.
    38  //
    39  // TODO(github.com/SagerNet/issue/6232): Remove after upgrading to containerd v1.4
    40  func ErrToGRPCf(err error, format string, args ...interface{}) error {
    41  	formatted := fmt.Sprintf(format, args...)
    42  	msg := fmt.Sprintf("%s: %s", formatted, err.Error())
    43  	return errToGRPCMsg(err, msg)
    44  }
    45  
    46  func errToGRPCMsg(err error, msg string) error {
    47  	if err == nil {
    48  		return nil
    49  	}
    50  	if _, ok := status.FromError(err); ok {
    51  		return err
    52  	}
    53  
    54  	switch {
    55  	case errors.Is(err, errdefs.ErrInvalidArgument):
    56  		return status.Errorf(codes.InvalidArgument, msg)
    57  	case errors.Is(err, errdefs.ErrNotFound):
    58  		return status.Errorf(codes.NotFound, msg)
    59  	case errors.Is(err, errdefs.ErrAlreadyExists):
    60  		return status.Errorf(codes.AlreadyExists, msg)
    61  	case errors.Is(err, errdefs.ErrFailedPrecondition):
    62  		return status.Errorf(codes.FailedPrecondition, msg)
    63  	case errors.Is(err, errdefs.ErrUnavailable):
    64  		return status.Errorf(codes.Unavailable, msg)
    65  	case errors.Is(err, errdefs.ErrNotImplemented):
    66  		return status.Errorf(codes.Unimplemented, msg)
    67  	case errors.Is(err, context.Canceled):
    68  		return status.Errorf(codes.Canceled, msg)
    69  	case errors.Is(err, context.DeadlineExceeded):
    70  		return status.Errorf(codes.DeadlineExceeded, msg)
    71  	}
    72  
    73  	return errdefs.ToGRPC(err)
    74  }