Pārlūkot izejas kodu

container: ViewDB: GetByPrefix() return typed errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Sebastiaan van Stijn 3 gadi atpakaļ
vecāks
revīzija
94dea2018e
2 mainītis faili ar 4 papildinājumiem un 22 dzēšanām
  1. 3 18
      container/view.go
  2. 1 4
      daemon/container.go

+ 3 - 18
container/view.go

@@ -30,21 +30,6 @@ var (
 	ErrNameNotReserved = errors.New("name is not reserved")
 )
 
-var (
-	// ErrEmptyPrefix is an error returned if the prefix was empty.
-	ErrEmptyPrefix = errors.New("Prefix can't be empty")
-)
-
-// ErrAmbiguousPrefix is returned if the prefix was ambiguous
-// (multiple ids for the prefix).
-type ErrAmbiguousPrefix struct {
-	prefix string
-}
-
-func (e ErrAmbiguousPrefix) Error() string {
-	return fmt.Sprintf("Multiple IDs found with provided prefix: %s", e.prefix)
-}
-
 // Snapshot is a read only view for Containers. It holds all information necessary to serve container queries in a
 // versioned ACID in-memory store.
 type Snapshot struct {
@@ -124,12 +109,12 @@ func NewViewDB() (*ViewDB, error) {
 // error if an empty prefix was given or if multiple containers match the prefix.
 func (db *ViewDB) GetByPrefix(s string) (string, error) {
 	if s == "" {
-		return "", ErrEmptyPrefix
+		return "", errdefs.InvalidParameter(errors.New("prefix can't be empty"))
 	}
 	txn := db.store.Txn(false)
 	iter, err := txn.Get(memdbContainersTable, memdbIDIndexPrefix, s)
 	if err != nil {
-		return "", err
+		return "", errdefs.System(err)
 	}
 
 	var (
@@ -142,7 +127,7 @@ func (db *ViewDB) GetByPrefix(s string) (string, error) {
 			break
 		}
 		if id != "" {
-			return "", ErrAmbiguousPrefix{prefix: s}
+			return "", errdefs.InvalidParameter(errors.New("multiple IDs found with provided prefix: " + s))
 		}
 		id = item.(*Container).ID
 	}

+ 1 - 4
daemon/container.go

@@ -50,10 +50,7 @@ func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, e
 
 	containerID, err := daemon.containersReplica.GetByPrefix(prefixOrName)
 	if err != nil {
-		if errdefs.IsNotFound(err) {
-			return nil, err
-		}
-		return nil, errdefs.System(err)
+		return nil, err
 	}
 	return daemon.containers.Get(containerID), nil
 }