X Tutup
Skip to content

Commit a4fadc5

Browse files
committed
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>
1 parent 917914f commit a4fadc5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+331
-555
lines changed

cmd/ctr/namespaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"strings"
99
"text/tabwriter"
1010

11+
"github.com/containerd/containerd/errdefs"
1112
"github.com/containerd/containerd/log"
12-
"github.com/containerd/containerd/metadata"
1313
"github.com/pkg/errors"
1414
"github.com/urfave/cli"
1515
)
@@ -183,7 +183,7 @@ var namespacesRemoveCommand = cli.Command{
183183

184184
for _, target := range clicontext.Args() {
185185
if err := namespaces.Delete(ctx, target); err != nil {
186-
if !metadata.IsNotFound(err) {
186+
if !errdefs.IsNotFound(err) {
187187
if exitErr == nil {
188188
exitErr = errors.Wrapf(err, "unable to delete %v", target)
189189
}

cmd/dist/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"fmt"
55

6-
"github.com/containerd/containerd/content"
6+
"github.com/containerd/containerd/errdefs"
77
"github.com/containerd/containerd/log"
88
digest "github.com/opencontainers/go-digest"
99
"github.com/urfave/cli"
@@ -43,7 +43,7 @@ var deleteCommand = cli.Command{
4343
}
4444

4545
if err := cs.Delete(ctx, dgst); err != nil {
46-
if !content.IsNotFound(err) {
46+
if !errdefs.IsNotFound(err) {
4747
if exitError == nil {
4848
exitError = err
4949
}

cmd/dist/fetch.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/containerd/containerd"
1313
"github.com/containerd/containerd/content"
14+
"github.com/containerd/containerd/errdefs"
1415
"github.com/containerd/containerd/images"
1516
"github.com/containerd/containerd/log"
1617
"github.com/containerd/containerd/progress"
@@ -153,7 +154,7 @@ outer:
153154
if !done && (!ok || status.Status == "downloading") {
154155
info, err := cs.Info(ctx, j.Digest)
155156
if err != nil {
156-
if !content.IsNotFound(err) {
157+
if !errdefs.IsNotFound(err) {
157158
log.G(ctx).WithError(err).Errorf("failed to get content info")
158159
continue outer
159160
} else {

cmd/dist/images.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"os"
66
"text/tabwriter"
77

8+
"github.com/containerd/containerd/errdefs"
89
"github.com/containerd/containerd/log"
9-
"github.com/containerd/containerd/metadata"
1010
"github.com/containerd/containerd/progress"
1111
"github.com/pkg/errors"
1212
"github.com/urfave/cli"
@@ -83,7 +83,7 @@ var imageRemoveCommand = cli.Command{
8383

8484
for _, target := range clicontext.Args() {
8585
if err := imageStore.Delete(ctx, target); err != nil {
86-
if !metadata.IsNotFound(err) {
86+
if !errdefs.IsNotFound(err) {
8787
if exitErr == nil {
8888
exitErr = errors.Wrapf(err, "unable to delete %v", target)
8989
}

container_unix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/containerd/containerd/api/services/tasks/v1"
1313
"github.com/containerd/containerd/api/types"
1414
"github.com/containerd/containerd/content"
15+
"github.com/containerd/containerd/errdefs"
1516
"github.com/containerd/containerd/images"
16-
"github.com/containerd/containerd/snapshot"
1717
protobuf "github.com/gogo/protobuf/types"
1818
digest "github.com/opencontainers/go-digest"
1919
"github.com/opencontainers/image-spec/identity"
@@ -46,7 +46,7 @@ func WithCheckpoint(desc v1.Descriptor, rootfsID string) NewContainerOpts {
4646
return err
4747
}
4848
if _, err := client.SnapshotService().Prepare(ctx, rootfsID, identity.ChainID(diffIDs).String()); err != nil {
49-
if !snapshot.IsExist(err) {
49+
if !errdefs.IsAlreadyExists(err) {
5050
return err
5151
}
5252
}

content/errors.go

Lines changed: 0 additions & 98 deletions
This file was deleted.

content/helpers.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"io/ioutil"
88

9+
"github.com/containerd/containerd/errdefs"
910
"github.com/opencontainers/go-digest"
1011
"github.com/pkg/errors"
1112
)
@@ -33,7 +34,7 @@ func ReadBlob(ctx context.Context, provider Provider, dgst digest.Digest) ([]byt
3334
func WriteBlob(ctx context.Context, cs Ingester, ref string, r io.Reader, size int64, expected digest.Digest) error {
3435
cw, err := cs.Writer(ctx, ref, size, expected)
3536
if err != nil {
36-
if !IsExists(err) {
37+
if !errdefs.IsAlreadyExists(err) {
3738
return err
3839
}
3940

@@ -79,7 +80,7 @@ func Copy(cw Writer, r io.Reader, size int64, expected digest.Digest) error {
7980
}
8081

8182
if err := cw.Commit(size, expected); err != nil {
82-
if !IsExists(err) {
83+
if !errdefs.IsAlreadyExists(err) {
8384
return errors.Wrapf(err, "failed commit on ref %q", ws.Ref)
8485
}
8586
}

content/locks.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package content
22

33
import (
4-
"fmt"
54
"sync"
5+
6+
"github.com/containerd/containerd/errdefs"
7+
"github.com/pkg/errors"
68
)
79

810
// Handles locking references
@@ -19,7 +21,7 @@ func tryLock(ref string) error {
1921
defer locksMu.Unlock()
2022

2123
if _, ok := locks[ref]; ok {
22-
return ErrLocked(fmt.Sprintf("key %s is locked", ref))
24+
return errors.Wrapf(errdefs.ErrUnavailable, "ref %s locked", ref)
2325
}
2426

2527
locks[ref] = struct{}{}

content/store.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"strconv"
1212
"time"
1313

14+
"github.com/containerd/containerd/errdefs"
1415
"github.com/containerd/containerd/log"
1516
digest "github.com/opencontainers/go-digest"
1617
"github.com/pkg/errors"
@@ -40,7 +41,7 @@ func (s *store) Info(ctx context.Context, dgst digest.Digest) (Info, error) {
4041
fi, err := os.Stat(p)
4142
if err != nil {
4243
if os.IsNotExist(err) {
43-
err = ErrNotFound("")
44+
err = errors.Wrapf(errdefs.ErrNotFound, "content %v", dgst)
4445
}
4546

4647
return Info{}, err
@@ -62,7 +63,7 @@ func (s *store) Reader(ctx context.Context, dgst digest.Digest) (io.ReadCloser,
6263
fp, err := os.Open(s.blobPath(dgst))
6364
if err != nil {
6465
if os.IsNotExist(err) {
65-
err = ErrNotFound("")
66+
err = errors.Wrapf(errdefs.ErrNotFound, "content %v", dgst)
6667
}
6768
return nil, err
6869
}
@@ -85,7 +86,7 @@ func (cs *store) Delete(ctx context.Context, dgst digest.Digest) error {
8586
return err
8687
}
8788

88-
return ErrNotFound("")
89+
return errors.Wrapf(errdefs.ErrNotFound, "content %v", dgst)
8990
}
9091

9192
return nil
@@ -232,7 +233,7 @@ func (s *store) Writer(ctx context.Context, ref string, total int64, expected di
232233
if expected != "" {
233234
p := s.blobPath(expected)
234235
if _, err := os.Stat(p); err == nil {
235-
return nil, ErrExists("")
236+
return nil, errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", expected)
236237
}
237238
}
238239

@@ -329,7 +330,7 @@ func (s *store) Abort(ctx context.Context, ref string) error {
329330
root := s.ingestRoot(ref)
330331
if err := os.RemoveAll(root); err != nil {
331332
if os.IsNotExist(err) {
332-
return ErrNotFound("")
333+
return errors.Wrapf(errdefs.ErrNotFound, "ingest ref %q", ref)
333334
}
334335

335336
return err

content/writer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66
"time"
77

8+
"github.com/containerd/containerd/errdefs"
89
"github.com/opencontainers/go-digest"
910
"github.com/pkg/errors"
1011
)
@@ -99,7 +100,7 @@ func (w *writer) Commit(size int64, expected digest.Digest) error {
99100
if err := os.Rename(ingest, target); err != nil {
100101
if os.IsExist(err) {
101102
// collision with the target file!
102-
return ErrExists("")
103+
return errors.Wrapf(errdefs.ErrAlreadyExists, "content %v", dgst)
103104
}
104105
return err
105106
}

0 commit comments

Comments
 (0)
X Tutup