container: ViewDB: GetByPrefix() return typed errors

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-07-08 13:23:01 +02:00
parent da4d627e79
commit 94dea2018e
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
2 changed files with 4 additions and 22 deletions

View file

@ -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
}

View file

@ -50,11 +50,8 @@ 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 daemon.containers.Get(containerID), nil
}