X Tutup
Skip to content

Commit ca2ec3e

Browse files
Merge pull request containerd#992 from estesp/errrbody-likes-errrrors
Use error interfaces for content/metadata
2 parents ac4c2ab + e10a9af commit ca2ec3e

File tree

14 files changed

+254
-70
lines changed

14 files changed

+254
-70
lines changed

content/content.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,9 @@ import (
77
"time"
88

99
"github.com/opencontainers/go-digest"
10-
"github.com/pkg/errors"
1110
)
1211

1312
var (
14-
// ErrNotFound is returned when an item is not found.
15-
//
16-
// Use IsNotFound(err) to detect this condition.
17-
ErrNotFound = errors.New("content: not found")
18-
19-
// ErrExists is returned when something exists when it may not be expected.
20-
//
21-
// Use IsExists(err) to detect this condition.
22-
ErrExists = errors.New("content: exists")
23-
24-
// ErrLocked is returned when content is actively being uploaded, this
25-
// indicates that another process is attempting to upload the same content.
26-
//
27-
// Use IsLocked(err) to detect this condition.
28-
ErrLocked = errors.New("content: locked")
29-
3013
bufPool = sync.Pool{
3114
New: func() interface{} {
3215
return make([]byte, 1<<20)
@@ -106,15 +89,3 @@ type Store interface {
10689
Ingester
10790
Provider
10891
}
109-
110-
func IsNotFound(err error) bool {
111-
return errors.Cause(err) == ErrNotFound
112-
}
113-
114-
func IsExists(err error) bool {
115-
return errors.Cause(err) == ErrExists
116-
}
117-
118-
func IsLocked(err error) bool {
119-
return errors.Cause(err) == ErrLocked
120-
}

content/errors.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package content
2+
3+
type contentExistsErr struct {
4+
desc string
5+
}
6+
7+
type contentNotFoundErr struct {
8+
desc string
9+
}
10+
11+
type contentLockedErr struct {
12+
desc string
13+
}
14+
15+
// ErrExists is returned when something exists when it may not be expected.
16+
func ErrExists(msg string) error {
17+
if msg == "" {
18+
msg = "content: exists"
19+
}
20+
return contentExistsErr{
21+
desc: msg,
22+
}
23+
}
24+
25+
// ErrNotFound is returned when an item is not found.
26+
func ErrNotFound(msg string) error {
27+
if msg == "" {
28+
msg = "content: not found"
29+
}
30+
return contentNotFoundErr{
31+
desc: msg,
32+
}
33+
}
34+
35+
// ErrLocked is returned when content is actively being uploaded, this
36+
// indicates that another process is attempting to upload the same content.
37+
func ErrLocked(msg string) error {
38+
if msg == "" {
39+
msg = "content: locked"
40+
}
41+
return contentLockedErr{
42+
desc: msg,
43+
}
44+
}
45+
46+
func (c contentExistsErr) Error() string {
47+
return c.desc
48+
}
49+
func (c contentNotFoundErr) Error() string {
50+
return c.desc
51+
}
52+
func (c contentLockedErr) Error() string {
53+
return c.desc
54+
}
55+
56+
func (c contentExistsErr) Exists() bool {
57+
return true
58+
}
59+
60+
func (c contentNotFoundErr) NotFound() bool {
61+
return true
62+
}
63+
64+
func (c contentLockedErr) Locked() bool {
65+
return true
66+
}
67+
68+
// IsNotFound returns true if the error is due to a not found content item
69+
func IsNotFound(err error) bool {
70+
if err, ok := err.(interface {
71+
NotFound() bool
72+
}); ok {
73+
return err.NotFound()
74+
}
75+
76+
causal, ok := err.(interface {
77+
Cause() error
78+
})
79+
if !ok {
80+
return false
81+
}
82+
83+
return IsNotFound(causal.Cause())
84+
}
85+
86+
// IsExists returns true if the error is due to an already existing content item
87+
func IsExists(err error) bool {
88+
if err, ok := err.(interface {
89+
Exists() bool
90+
}); ok {
91+
return err.Exists()
92+
}
93+
94+
causal, ok := err.(interface {
95+
Cause() error
96+
})
97+
if !ok {
98+
return false
99+
}
100+
101+
return IsExists(causal.Cause())
102+
}
103+
104+
// IsLocked returns true if the error is due to a currently locked content item
105+
func IsLocked(err error) bool {
106+
if err, ok := err.(interface {
107+
Locked() bool
108+
}); ok {
109+
return err.Locked()
110+
}
111+
112+
causal, ok := err.(interface {
113+
Cause() error
114+
})
115+
if !ok {
116+
return false
117+
}
118+
119+
return IsLocked(causal.Cause())
120+
}

content/locks.go

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

33
import (
4+
"fmt"
45
"sync"
5-
6-
"github.com/pkg/errors"
76
)
87

98
// Handles locking references
@@ -20,7 +19,7 @@ func tryLock(ref string) error {
2019
defer locksMu.Unlock()
2120

2221
if _, ok := locks[ref]; ok {
23-
return errors.Wrapf(ErrLocked, "key %s is locked", ref)
22+
return ErrLocked(fmt.Sprintf("key %s is locked", ref))
2423
}
2524

2625
locks[ref] = struct{}{}

content/store.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (s *store) Info(ctx context.Context, dgst digest.Digest) (Info, error) {
4040
fi, err := os.Stat(p)
4141
if err != nil {
4242
if os.IsNotExist(err) {
43-
err = ErrNotFound
43+
err = ErrNotFound("")
4444
}
4545

4646
return Info{}, err
@@ -62,7 +62,7 @@ func (s *store) Reader(ctx context.Context, dgst digest.Digest) (io.ReadCloser,
6262
fp, err := os.Open(s.blobPath(dgst))
6363
if err != nil {
6464
if os.IsNotExist(err) {
65-
err = ErrNotFound
65+
err = ErrNotFound("")
6666
}
6767
return nil, err
6868
}
@@ -85,7 +85,7 @@ func (cs *store) Delete(ctx context.Context, dgst digest.Digest) error {
8585
return err
8686
}
8787

88-
return ErrNotFound
88+
return ErrNotFound("")
8989
}
9090

9191
return nil
@@ -329,7 +329,7 @@ func (s *store) Abort(ctx context.Context, ref string) error {
329329
root := s.ingestRoot(ref)
330330
if err := os.RemoveAll(root); err != nil {
331331
if os.IsNotExist(err) {
332-
return ErrNotFound
332+
return ErrNotFound("")
333333
}
334334

335335
return err

content/writer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (w *writer) Commit(size int64, expected digest.Digest) error {
9999
if err := os.Rename(ingest, target); err != nil {
100100
if os.IsExist(err) {
101101
// collision with the target file!
102-
return ErrExists
102+
return ErrExists("")
103103
}
104104
return err
105105
}

metadata/containers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (s *containerStore) Get(ctx context.Context, id string) (containers.Contain
2828

2929
bkt := getContainerBucket(s.tx, namespace, id)
3030
if bkt == nil {
31-
return containers.Container{}, errors.Wrap(ErrNotFound, "bucket does not exist")
31+
return containers.Container{}, ErrNotFound("bucket does not exist")
3232
}
3333

3434
container := containers.Container{ID: id}
@@ -85,7 +85,7 @@ func (s *containerStore) Create(ctx context.Context, container containers.Contai
8585
cbkt, err := bkt.CreateBucket([]byte(container.ID))
8686
if err != nil {
8787
if err == bolt.ErrBucketExists {
88-
err = errors.Wrap(ErrExists, "content for id already exists")
88+
err = ErrExists("content for id already exists")
8989
}
9090
return containers.Container{}, err
9191
}
@@ -107,12 +107,12 @@ func (s *containerStore) Update(ctx context.Context, container containers.Contai
107107

108108
bkt := getContainersBucket(s.tx, namespace)
109109
if bkt == nil {
110-
return containers.Container{}, errors.Wrap(ErrNotFound, "no containers")
110+
return containers.Container{}, ErrNotFound("no containers")
111111
}
112112

113113
cbkt := bkt.Bucket([]byte(container.ID))
114114
if cbkt == nil {
115-
return containers.Container{}, errors.Wrap(ErrNotFound, "no content for id")
115+
return containers.Container{}, ErrNotFound("no content for id")
116116
}
117117

118118
container.UpdatedAt = time.Now()
@@ -131,11 +131,11 @@ func (s *containerStore) Delete(ctx context.Context, id string) error {
131131

132132
bkt := getContainersBucket(s.tx, namespace)
133133
if bkt == nil {
134-
return errors.Wrap(ErrNotFound, "no containers")
134+
return ErrNotFound("no containers")
135135
}
136136

137137
if err := bkt.DeleteBucket([]byte(id)); err == bolt.ErrBucketNotFound {
138-
return errors.Wrap(ErrNotFound, "no content for id")
138+
return ErrNotFound("no content for id")
139139
}
140140
return err
141141
}

0 commit comments

Comments
 (0)
X Tutup