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

@@ -1,98 +0,0 @@
package content
import "github.com/pkg/errors"
type contentExistsErr struct {
desc string
}
type contentNotFoundErr struct {
desc string
}
type contentLockedErr struct {
desc string
}
// ErrExists is returned when something exists when it may not be expected.
func ErrExists(msg string) error {
if msg == "" {
msg = "content: exists"
}
return errors.WithStack(contentExistsErr{
desc: msg,
})
}
// ErrNotFound is returned when an item is not found.
func ErrNotFound(msg string) error {
if msg == "" {
msg = "content: not found"
}
return errors.WithStack(contentNotFoundErr{
desc: msg,
})
}
// ErrLocked is returned when content is actively being uploaded, this
// indicates that another process is attempting to upload the same content.
func ErrLocked(msg string) error {
if msg == "" {
msg = "content: locked"
}
return errors.WithStack(contentLockedErr{
desc: msg,
})
}
func (c contentExistsErr) Error() string {
return c.desc
}
func (c contentNotFoundErr) Error() string {
return c.desc
}
func (c contentLockedErr) Error() string {
return c.desc
}
func (c contentExistsErr) Exists() bool {
return true
}
func (c contentNotFoundErr) NotFound() bool {
return true
}
func (c contentLockedErr) Locked() bool {
return true
}
// IsNotFound returns true if the error is due to a not found content 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 content item
func IsExists(err error) bool {
if err, ok := errors.Cause(err).(interface {
Exists() bool
}); ok {
return err.Exists()
}
return false
}
// IsLocked returns true if the error is due to a currently locked content item
func IsLocked(err error) bool {
if err, ok := errors.Cause(err).(interface {
Locked() bool
}); ok {
return err.Locked()
}
return false
}

View File

@@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"github.com/containerd/containerd/errdefs"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
@@ -33,7 +34,7 @@ func ReadBlob(ctx context.Context, provider Provider, dgst digest.Digest) ([]byt
func WriteBlob(ctx context.Context, cs Ingester, ref string, r io.Reader, size int64, expected digest.Digest) error {
cw, err := cs.Writer(ctx, ref, size, expected)
if err != nil {
if !IsExists(err) {
if !errdefs.IsAlreadyExists(err) {
return err
}
@@ -79,7 +80,7 @@ func Copy(cw Writer, r io.Reader, size int64, expected digest.Digest) error {
}
if err := cw.Commit(size, expected); err != nil {
if !IsExists(err) {
if !errdefs.IsAlreadyExists(err) {
return errors.Wrapf(err, "failed commit on ref %q", ws.Ref)
}
}

View File

@@ -1,8 +1,10 @@
package content
import (
"fmt"
"sync"
"github.com/containerd/containerd/errdefs"
"github.com/pkg/errors"
)
// Handles locking references
@@ -19,7 +21,7 @@ func tryLock(ref string) error {
defer locksMu.Unlock()
if _, ok := locks[ref]; ok {
return ErrLocked(fmt.Sprintf("key %s is locked", ref))
return errors.Wrapf(errdefs.ErrUnavailable, "ref %s locked", ref)
}
locks[ref] = struct{}{}

View File

@@ -11,6 +11,7 @@ import (
"strconv"
"time"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
digest "github.com/opencontainers/go-digest"
"github.com/pkg/errors"
@@ -40,7 +41,7 @@ func (s *store) Info(ctx context.Context, dgst digest.Digest) (Info, error) {
fi, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
err = ErrNotFound("")
err = errors.Wrapf(errdefs.ErrNotFound, "content %v", dgst)
}
return Info{}, err
@@ -62,7 +63,7 @@ func (s *store) Reader(ctx context.Context, dgst digest.Digest) (io.ReadCloser,
fp, err := os.Open(s.blobPath(dgst))
if err != nil {
if os.IsNotExist(err) {
err = ErrNotFound("")
err = errors.Wrapf(errdefs.ErrNotFound, "content %v", dgst)
}
return nil, err
}
@@ -85,7 +86,7 @@ func (cs *store) Delete(ctx context.Context, dgst digest.Digest) error {
return err
}
return ErrNotFound("")
return errors.Wrapf(errdefs.ErrNotFound, "content %v", dgst)
}
return nil
@@ -232,7 +233,7 @@ func (s *store) Writer(ctx context.Context, ref string, total int64, expected di
if expected != "" {
p := s.blobPath(expected)
if _, err := os.Stat(p); err == nil {
return nil, ErrExists("")
return nil, errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", expected)
}
}
@@ -329,7 +330,7 @@ func (s *store) Abort(ctx context.Context, ref string) error {
root := s.ingestRoot(ref)
if err := os.RemoveAll(root); err != nil {
if os.IsNotExist(err) {
return ErrNotFound("")
return errors.Wrapf(errdefs.ErrNotFound, "ingest ref %q", ref)
}
return err

View File

@@ -5,6 +5,7 @@ import (
"path/filepath"
"time"
"github.com/containerd/containerd/errdefs"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
@@ -99,7 +100,7 @@ func (w *writer) Commit(size int64, expected digest.Digest) error {
if err := os.Rename(ingest, target); err != nil {
if os.IsExist(err) {
// collision with the target file!
return ErrExists("")
return errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", dgst)
}
return err
}