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:
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/boltdb/bolt"
|
||||
"github.com/containerd/containerd/errdefs"
|
||||
"github.com/containerd/containerd/namespaces"
|
||||
"github.com/containerd/containerd/snapshot"
|
||||
db "github.com/containerd/containerd/snapshot/storage/proto"
|
||||
@@ -129,12 +130,12 @@ func CreateActive(ctx context.Context, key, parent string, readonly bool) (a Act
|
||||
}
|
||||
|
||||
if parentS.Kind != db.KindCommitted {
|
||||
return errors.Wrap(snapshot.ErrSnapshotNotCommitted, "parent is not committed snapshot")
|
||||
return errors.Wrap(errdefs.ErrInvalidArgument, "parent is not committed snapshot")
|
||||
}
|
||||
}
|
||||
b := bkt.Get([]byte(key))
|
||||
if len(b) != 0 {
|
||||
return snapshot.ErrSnapshotExist
|
||||
return errors.Wrapf(errdefs.ErrAlreadyExists, "snapshot %v", key)
|
||||
}
|
||||
|
||||
id, err := nextSequence(ctx)
|
||||
@@ -183,7 +184,7 @@ func GetActive(ctx context.Context, key string) (a Active, err error) {
|
||||
err = withBucket(ctx, func(ctx context.Context, bkt, pbkt *bolt.Bucket) error {
|
||||
b := bkt.Get([]byte(key))
|
||||
if len(b) == 0 {
|
||||
return snapshot.ErrSnapshotNotExist
|
||||
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v", key)
|
||||
}
|
||||
|
||||
var ss db.Snapshot
|
||||
@@ -191,7 +192,7 @@ func GetActive(ctx context.Context, key string) (a Active, err error) {
|
||||
return errors.Wrap(err, "failed to unmarshal snapshot")
|
||||
}
|
||||
if ss.Kind != db.KindActive {
|
||||
return snapshot.ErrSnapshotNotActive
|
||||
return errors.Wrapf(errdefs.ErrFailedPrecondition, "requested snapshot %v not active", key)
|
||||
}
|
||||
|
||||
a.ID = fmt.Sprintf("%d", ss.ID)
|
||||
@@ -225,7 +226,7 @@ func Remove(ctx context.Context, key string) (id string, k snapshot.Kind, err er
|
||||
var ss db.Snapshot
|
||||
b := bkt.Get([]byte(key))
|
||||
if len(b) == 0 {
|
||||
return snapshot.ErrSnapshotNotExist
|
||||
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v", key)
|
||||
}
|
||||
|
||||
if err := proto.Unmarshal(b, &ss); err != nil {
|
||||
@@ -273,7 +274,7 @@ func CommitActive(ctx context.Context, key, name string, usage snapshot.Usage) (
|
||||
err = withBucket(ctx, func(ctx context.Context, bkt, pbkt *bolt.Bucket) error {
|
||||
b := bkt.Get([]byte(name))
|
||||
if len(b) != 0 {
|
||||
return errors.Wrap(snapshot.ErrSnapshotExist, "committed name already exists")
|
||||
return errors.Wrapf(errdefs.ErrAlreadyExists, "committed snapshot %v", name)
|
||||
}
|
||||
|
||||
var ss db.Snapshot
|
||||
@@ -281,7 +282,7 @@ func CommitActive(ctx context.Context, key, name string, usage snapshot.Usage) (
|
||||
return errors.Wrap(err, "failed to get active snapshot")
|
||||
}
|
||||
if ss.Kind != db.KindActive {
|
||||
return snapshot.ErrSnapshotNotActive
|
||||
return errors.Wrapf(errdefs.ErrFailedPrecondition, "snapshot %v is not active", name)
|
||||
}
|
||||
if ss.Readonly {
|
||||
return errors.Errorf("active snapshot is readonly")
|
||||
@@ -340,12 +341,12 @@ func withBucket(ctx context.Context, fn func(context.Context, *bolt.Bucket, *bol
|
||||
}
|
||||
nbkt := t.tx.Bucket(bucketKeyStorageVersion)
|
||||
if nbkt == nil {
|
||||
return errors.Wrap(snapshot.ErrSnapshotNotExist, "bucket does not exist")
|
||||
return errors.Wrapf(errdefs.ErrNotFound, "bucket does not exist")
|
||||
}
|
||||
|
||||
bkt := nbkt.Bucket([]byte(namespace))
|
||||
if bkt == nil {
|
||||
return errors.Wrap(snapshot.ErrSnapshotNotExist, "namespace not available in snapshotter")
|
||||
return errors.Wrapf(errdefs.ErrNotFound, "namespace not available in snapshotter")
|
||||
}
|
||||
|
||||
return fn(ctx, bkt.Bucket(bucketKeySnapshot), bkt.Bucket(bucketKeyParents))
|
||||
@@ -409,7 +410,7 @@ func parents(bkt *bolt.Bucket, parent *db.Snapshot) (parents []string, err error
|
||||
func getSnapshot(bkt *bolt.Bucket, key string, ss *db.Snapshot) error {
|
||||
b := bkt.Get([]byte(key))
|
||||
if len(b) == 0 {
|
||||
return snapshot.ErrSnapshotNotExist
|
||||
return errors.Wrapf(errdefs.ErrNotFound, "snapshot %v", key)
|
||||
}
|
||||
if err := proto.Unmarshal(b, ss); err != nil {
|
||||
return errors.Wrap(err, "failed to unmarshal snapshot")
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/containerd/containerd/errdefs"
|
||||
"github.com/containerd/containerd/namespaces"
|
||||
"github.com/containerd/containerd/snapshot"
|
||||
"github.com/pkg/errors"
|
||||
@@ -205,7 +206,7 @@ func assertNotExist(t *testing.T, err error) {
|
||||
if err == nil {
|
||||
t.Fatal("Expected not exist error")
|
||||
}
|
||||
if !snapshot.IsNotExist(err) {
|
||||
if !errdefs.IsNotFound(err) {
|
||||
t.Fatalf("Expected not exist error, got %+v", err)
|
||||
}
|
||||
}
|
||||
@@ -214,7 +215,7 @@ func assertNotActive(t *testing.T, err error) {
|
||||
if err == nil {
|
||||
t.Fatal("Expected not active error")
|
||||
}
|
||||
if !snapshot.IsNotActive(err) {
|
||||
if !errdefs.IsFailedPrecondition(err) {
|
||||
t.Fatalf("Expected not active error, got %+v", err)
|
||||
}
|
||||
}
|
||||
@@ -223,7 +224,7 @@ func assertNotCommitted(t *testing.T, err error) {
|
||||
if err == nil {
|
||||
t.Fatal("Expected active error")
|
||||
}
|
||||
if !snapshot.IsNotCommitted(err) {
|
||||
if !errdefs.IsInvalidArgument(err) {
|
||||
t.Fatalf("Expected active error, got %+v", err)
|
||||
}
|
||||
}
|
||||
@@ -232,7 +233,7 @@ func assertExist(t *testing.T, err error) {
|
||||
if err == nil {
|
||||
t.Fatal("Expected exist error")
|
||||
}
|
||||
if !snapshot.IsExist(err) {
|
||||
if !errdefs.IsAlreadyExists(err) {
|
||||
t.Fatalf("Expected exist error, got %+v", err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user