
These interfaces allow us to preserve both the checking of error "cause" as well as messages returned from the gRPC API so that the client gets full error reason instead of a default "metadata: not found" in the case of a missing image. Signed-off-by: Phil Estes <estesp@linux.vnet.ibm.com>
37 lines
837 B
Go
37 lines
837 B
Go
package namespaces
|
|
|
|
import (
|
|
"github.com/containerd/containerd/metadata"
|
|
"github.com/pkg/errors"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/codes"
|
|
)
|
|
|
|
func mapGRPCError(err error, id string) error {
|
|
switch {
|
|
case metadata.IsNotFound(err):
|
|
return grpc.Errorf(codes.NotFound, "namespace %v not found", id)
|
|
case metadata.IsExists(err):
|
|
return grpc.Errorf(codes.AlreadyExists, "namespace %v already exists", id)
|
|
case metadata.IsNotEmpty(err):
|
|
return grpc.Errorf(codes.FailedPrecondition, "namespace %v must be empty", id)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func rewriteGRPCError(err error) error {
|
|
if err == nil {
|
|
return err
|
|
}
|
|
|
|
switch grpc.Code(errors.Cause(err)) {
|
|
case codes.AlreadyExists:
|
|
return metadata.ErrExists(grpc.ErrorDesc(err))
|
|
case codes.NotFound:
|
|
return metadata.ErrNotFound(grpc.ErrorDesc(err))
|
|
}
|
|
|
|
return err
|
|
}
|