Files
kata-containers/containerd-shim-v2/errors.go
Peng Tao 8215a3ce9a shimv2: convert vc errors to grpc errors
containerd checks for the grpc error code to determine
correct recover action upon grpc errors. We need to provide
them properly.

Unfortunately ttrpc doesn't support grpc interceptor so we have
to modify every service function for it.

Fixes: #1527

Signed-off-by: Peng Tao <bergwolf@hyper.sh>
2019-04-12 03:57:01 -07:00

63 lines
1.5 KiB
Go

// Copyright (c) 2019 hyper.sh
//
// SPDX-License-Identifier: Apache-2.0
//
package containerdshim
import (
"strings"
"syscall"
"github.com/pkg/errors"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
vc "github.com/kata-containers/runtime/virtcontainers/pkg/types"
)
// toGRPC maps the virtcontainers error into a grpc error,
// using the original error message as a description.
func toGRPC(err error) error {
if err == nil {
return nil
}
if isGRPCError(err) {
// error has already been mapped to grpc
return err
}
err = errors.Cause(err)
switch {
case isInvalidArgument(err):
return status.Errorf(codes.InvalidArgument, err.Error())
case isNotFound(err):
return status.Errorf(codes.NotFound, err.Error())
}
return err
}
// toGRPCf maps the error to grpc error codes, assembling the formatting string
// and combining it with the target error string.
func toGRPCf(err error, format string, args ...interface{}) error {
return toGRPC(errors.Wrapf(err, format, args...))
}
func isGRPCError(err error) bool {
_, ok := status.FromError(err)
return ok
}
func isInvalidArgument(err error) bool {
return err == vc.ErrNeedSandbox || err == vc.ErrNeedSandboxID ||
err == vc.ErrNeedContainerID || err == vc.ErrNeedState ||
err == syscall.EINVAL
}
func isNotFound(err error) bool {
return err == vc.ErrNoSuchContainer || err == syscall.ENOENT ||
strings.Contains(err.Error(), "not found") || strings.Contains(err.Error(), "not exist")
}