errdefs: centralize error handling

Now that we have most of the services required for use with containerd,
it was found that common patterns were used throughout services. By
defining a central `errdefs` package, we ensure that services will map
errors to and from grpc consistently and cleanly. One can decorate an
error with as much context as necessary, using `pkg/errors` and still
have the error mapped correctly via grpc.

We make a few sacrifices. At this point, the common errors we use across
the repository all map directly to grpc error codes. While this seems
positively crazy, it actually works out quite well. The error conditions
that were specific weren't super necessary and the ones that were
necessary now simply have better context information. We lose the
ability to add new codes, but this constraint may not be a bad thing.

Effectively, as long as one uses the errors defined in `errdefs`, the
error class will be mapped correctly across the grpc boundary and
everything will be good. If you don't use those definitions, the error
maps to "unknown" and the error message is preserved.

Signed-off-by: Stephen J Day <stephen.day@docker.com>
This commit is contained in:
Stephen J Day
2017-06-28 21:32:15 -07:00
parent 917914fcf5
commit a4fadc596b
42 changed files with 331 additions and 555 deletions

View File

@@ -6,6 +6,7 @@ import (
"github.com/boltdb/bolt"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/identifiers"
"github.com/containerd/containerd/namespaces"
"github.com/pkg/errors"
@@ -29,12 +30,12 @@ func (s *containerStore) Get(ctx context.Context, id string) (containers.Contain
bkt := getContainerBucket(s.tx, namespace, id)
if bkt == nil {
return containers.Container{}, ErrNotFound("bucket does not exist")
return containers.Container{}, errors.Wrapf(errdefs.ErrNotFound, "bucket name %q")
}
container := containers.Container{ID: id}
if err := readContainer(&container, bkt); err != nil {
return containers.Container{}, errors.Wrap(err, "failed to read container")
return containers.Container{}, errors.Wrapf(err, "failed to read container %v", id)
}
return container, nil
@@ -90,7 +91,7 @@ func (s *containerStore) Create(ctx context.Context, container containers.Contai
cbkt, err := bkt.CreateBucket([]byte(container.ID))
if err != nil {
if err == bolt.ErrBucketExists {
err = ErrExists("content for id already exists")
err = errors.Wrapf(errdefs.ErrAlreadyExists, "content %q")
}
return containers.Container{}, err
}
@@ -110,14 +111,18 @@ func (s *containerStore) Update(ctx context.Context, container containers.Contai
return containers.Container{}, err
}
if container.ID == "" {
return containers.Container{}, errors.Wrapf(errdefs.ErrInvalidArgument, "must specify a container id")
}
bkt := getContainersBucket(s.tx, namespace)
if bkt == nil {
return containers.Container{}, ErrNotFound("no containers")
return containers.Container{}, errors.Wrapf(errdefs.ErrNotFound, "container %q", container.ID)
}
cbkt := bkt.Bucket([]byte(container.ID))
if cbkt == nil {
return containers.Container{}, ErrNotFound("no content for id")
return containers.Container{}, errors.Wrapf(errdefs.ErrNotFound, "container %q", container.ID)
}
container.UpdatedAt = time.Now()
@@ -136,11 +141,11 @@ func (s *containerStore) Delete(ctx context.Context, id string) error {
bkt := getContainersBucket(s.tx, namespace)
if bkt == nil {
return ErrNotFound("no containers")
return errors.Wrapf(errdefs.ErrNotFound, "cannot delete container %v, bucket not present", id)
}
if err := bkt.DeleteBucket([]byte(id)); err == bolt.ErrBucketNotFound {
return ErrNotFound("no content for id")
return errors.Wrapf(errdefs.ErrNotFound, "container %v", id)
}
return err
}

View File

@@ -1,95 +0,0 @@
package metadata
import "github.com/pkg/errors"
type metadataExistsErr struct {
desc string
}
type metadataNotFoundErr struct {
desc string
}
type metadataNotEmptyErr struct {
desc string
}
// ErrExists is returned when an item already exists in metadata
func ErrExists(msg string) error {
if msg == "" {
msg = "metadata: exists"
}
return errors.WithStack(metadataExistsErr{
desc: msg,
})
}
// ErrNotFound is returned when an item cannot be found in metadata
func ErrNotFound(msg string) error {
if msg == "" {
msg = "metadata: not found"
}
return errors.WithStack(metadataNotFoundErr{
desc: msg,
})
}
// ErrNotEmpty is returned when a metadata item can't be deleted because it is not empty
func ErrNotEmpty(msg string) error {
if msg == "" {
msg = "metadata: namespace not empty"
}
return errors.WithStack(metadataNotEmptyErr{
desc: msg,
})
}
func (m metadataExistsErr) Error() string {
return m.desc
}
func (m metadataNotFoundErr) Error() string {
return m.desc
}
func (m metadataNotEmptyErr) Error() string {
return m.desc
}
func (m metadataExistsErr) Exists() bool {
return true
}
func (m metadataNotFoundErr) NotFound() bool {
return true
}
func (m metadataNotEmptyErr) NotEmpty() bool {
return true
}
// IsNotFound returns true if the error is due to a missing metadata item
func IsNotFound(err error) bool {
if err, ok := errors.Cause(err).(interface {
NotFound() bool
}); ok {
return err.NotFound()
}
return false
}
// IsExists returns true if the error is due to an already existing metadata item
func IsExists(err error) bool {
if err, ok := errors.Cause(err).(interface {
Exists() bool
}); ok {
return err.Exists()
}
return false
}
// IsNotEmpty returns true if the error is due to delete request of a non-empty metadata item
func IsNotEmpty(err error) bool {
if err, ok := errors.Cause(err).(interface {
NotEmpty() bool
}); ok {
return err.NotEmpty()
}
return false
}

View File

@@ -6,10 +6,12 @@ import (
"fmt"
"github.com/boltdb/bolt"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/namespaces"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
)
type imageStore struct {
@@ -30,12 +32,12 @@ func (s *imageStore) Get(ctx context.Context, name string) (images.Image, error)
bkt := getImagesBucket(s.tx, namespace)
if bkt == nil {
return images.Image{}, ErrNotFound("")
return images.Image{}, errors.Wrapf(errdefs.ErrNotFound, "image %q", name)
}
ibkt := bkt.Bucket([]byte(name))
if ibkt == nil {
return images.Image{}, ErrNotFound("")
return images.Image{}, errors.Wrapf(errdefs.ErrNotFound, "image %q", name)
}
image.Name = name
@@ -124,7 +126,7 @@ func (s *imageStore) Delete(ctx context.Context, name string) error {
return withImagesBucket(s.tx, namespace, func(bkt *bolt.Bucket) error {
err := bkt.DeleteBucket([]byte(name))
if err == bolt.ErrBucketNotFound {
return ErrNotFound("")
return errors.Wrapf(errdefs.ErrNotFound, "image %q", name)
}
return err
})

View File

@@ -4,8 +4,10 @@ import (
"context"
"github.com/boltdb/bolt"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/identifiers"
"github.com/containerd/containerd/namespaces"
"github.com/pkg/errors"
)
type namespaceStore struct {
@@ -30,7 +32,7 @@ func (s *namespaceStore) Create(ctx context.Context, namespace string, labels ma
bkt, err := topbkt.CreateBucket([]byte(namespace))
if err != nil {
if err == bolt.ErrBucketExists {
return ErrExists("")
return errors.Wrapf(errdefs.ErrAlreadyExists, "namespace %q")
}
return err
@@ -105,12 +107,12 @@ func (s *namespaceStore) Delete(ctx context.Context, namespace string) error {
if empty, err := s.namespaceEmpty(ctx, namespace); err != nil {
return err
} else if !empty {
return ErrNotEmpty("")
return errors.Wrapf(errdefs.ErrFailedPrecondition, "namespace %q must be empty", namespace)
}
if err := bkt.DeleteBucket([]byte(namespace)); err != nil {
if err == bolt.ErrBucketNotFound {
return ErrNotFound("")
return errors.Wrapf(errdefs.ErrNotFound, "namespace %q", namespace)
}
return err