mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-28 19:44:21 +01:00
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>
63 lines
1.5 KiB
Go
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")
|
|
}
|