diff --git a/container/view.go b/container/view.go index 4c9a48ea8a..9b63c74eef 100644 --- a/container/view.go +++ b/container/view.go @@ -9,6 +9,7 @@ import ( "github.com/docker/docker/api/types" "github.com/docker/docker/api/types/network" + "github.com/docker/docker/errdefs" "github.com/docker/go-connections/nat" memdb "github.com/hashicorp/go-memdb" "github.com/sirupsen/logrus" @@ -32,9 +33,6 @@ var ( var ( // ErrEmptyPrefix is an error returned if the prefix was empty. ErrEmptyPrefix = errors.New("Prefix can't be empty") - - // ErrNotExist is returned when ID or its prefix not found in index. - ErrNotExist = errors.New("ID does not exist") ) // ErrAmbiguousPrefix is returned if the prefix was ambiguous @@ -113,17 +111,6 @@ type ViewDB struct { store *memdb.MemDB } -// NoSuchContainerError indicates that the container wasn't found in the -// database. -type NoSuchContainerError struct { - id string -} - -// Error satisfies the error interface. -func (e NoSuchContainerError) Error() string { - return "no such container " + e.id -} - // NewViewDB provides the default implementation, with the default schema func NewViewDB() (*ViewDB, error) { store, err := memdb.NewMemDB(schema) @@ -164,7 +151,7 @@ func (db *ViewDB) GetByPrefix(s string) (string, error) { return id, nil } - return "", ErrNotExist + return "", errdefs.NotFound(errors.New("No such container: " + s)) } // Snapshot provides a consistent read-only view of the database. @@ -268,7 +255,7 @@ func (v *View) Get(id string) (*Snapshot, error) { return nil, err } if s == nil { - return nil, NoSuchContainerError{id: id} + return nil, errdefs.NotFound(errors.New("No such container: " + id)) } return v.transform(s.(*Container)), nil } diff --git a/daemon/container.go b/daemon/container.go index d21b3386e1..75d6c48a26 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -48,13 +48,12 @@ func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, e return containerByName, nil } - containerID, indexError := daemon.containersReplica.GetByPrefix(prefixOrName) - if indexError != nil { - // When truncindex defines an error type, use that instead - if indexError == container.ErrNotExist { - return nil, containerNotFound(prefixOrName) + containerID, err := daemon.containersReplica.GetByPrefix(prefixOrName) + if err != nil { + if errdefs.IsNotFound(err) { + return nil, err } - return nil, errdefs.System(indexError) + return nil, errdefs.System(err) } return daemon.containers.Get(containerID), nil } diff --git a/daemon/list.go b/daemon/list.go index b85f8d6b48..c7776816ee 100644 --- a/daemon/list.go +++ b/daemon/list.go @@ -162,14 +162,14 @@ func (daemon *Daemon) filterByNameIDMatches(view *container.View, filter *listCo cntrs := make([]container.Snapshot, 0, len(matches)) for id := range matches { c, err := view.Get(id) - switch err.(type) { - case nil: - cntrs = append(cntrs, *c) - case container.NoSuchContainerError: - // ignore error - default: + if err != nil { + if errdefs.IsNotFound(err) { + // ignore error + continue + } return nil, err } + cntrs = append(cntrs, *c) } // Restore sort-order after filtering @@ -367,8 +367,7 @@ func (daemon *Daemon) foldFilter(ctx context.Context, view *container.View, conf func idOrNameFilter(view *container.View, value string) (*container.Snapshot, error) { filter, err := view.Get(value) - switch err.(type) { - case container.NoSuchContainerError: + if err != nil && errdefs.IsNotFound(err) { // Try name search instead found := "" for id, idNames := range view.GetAllNames() {