Merge pull request #992 from estesp/errrbody-likes-errrrors

Use error interfaces for content/metadata
This commit is contained in:
Michael Crosby
2017-06-21 11:55:11 -07:00
committed by GitHub
14 changed files with 254 additions and 70 deletions

View File

@@ -7,26 +7,9 @@ import (
"time"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
var (
// ErrNotFound is returned when an item is not found.
//
// Use IsNotFound(err) to detect this condition.
ErrNotFound = errors.New("content: not found")
// ErrExists is returned when something exists when it may not be expected.
//
// Use IsExists(err) to detect this condition.
ErrExists = errors.New("content: exists")
// ErrLocked is returned when content is actively being uploaded, this
// indicates that another process is attempting to upload the same content.
//
// Use IsLocked(err) to detect this condition.
ErrLocked = errors.New("content: locked")
bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 1<<20)
@@ -106,15 +89,3 @@ type Store interface {
Ingester
Provider
}
func IsNotFound(err error) bool {
return errors.Cause(err) == ErrNotFound
}
func IsExists(err error) bool {
return errors.Cause(err) == ErrExists
}
func IsLocked(err error) bool {
return errors.Cause(err) == ErrLocked
}

120
content/errors.go Normal file
View File

@@ -0,0 +1,120 @@
package content
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 contentExistsErr{
desc: msg,
}
}
// ErrNotFound is returned when an item is not found.
func ErrNotFound(msg string) error {
if msg == "" {
msg = "content: not found"
}
return 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 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 := err.(interface {
NotFound() bool
}); ok {
return err.NotFound()
}
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false
}
return IsNotFound(causal.Cause())
}
// IsExists returns true if the error is due to an already existing content item
func IsExists(err error) bool {
if err, ok := err.(interface {
Exists() bool
}); ok {
return err.Exists()
}
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false
}
return IsExists(causal.Cause())
}
// IsLocked returns true if the error is due to a currently locked content item
func IsLocked(err error) bool {
if err, ok := err.(interface {
Locked() bool
}); ok {
return err.Locked()
}
causal, ok := err.(interface {
Cause() error
})
if !ok {
return false
}
return IsLocked(causal.Cause())
}

View File

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

View File

@@ -40,7 +40,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 = ErrNotFound("")
}
return Info{}, err
@@ -62,7 +62,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 = ErrNotFound("")
}
return nil, err
}
@@ -85,7 +85,7 @@ func (cs *store) Delete(ctx context.Context, dgst digest.Digest) error {
return err
}
return ErrNotFound
return ErrNotFound("")
}
return nil
@@ -329,7 +329,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 ErrNotFound("")
}
return err

View File

@@ -99,7 +99,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 ErrExists("")
}
return err
}