From d131147a5cc0f2972f52b487a4b966013749c145 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 25 Nov 2022 13:26:46 +0100 Subject: [PATCH] use errors.Is() to handle image store errors The image store's used are an interface, so there's no guarantee that implementations don't wrap the errors. Make sure to catch such cases by using errors.Is. Signed-off-by: Sebastiaan van Stijn --- daemon/images/image_list.go | 3 ++- image/store.go | 2 +- layer/layer_test.go | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/daemon/images/image_list.go b/daemon/images/image_list.go index 2686b3f8f5..8ab56a6820 100644 --- a/daemon/images/image_list.go +++ b/daemon/images/image_list.go @@ -2,6 +2,7 @@ package images // import "github.com/docker/docker/daemon/images" import ( "context" + "errors" "fmt" "sort" "time" @@ -132,7 +133,7 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions) if err != nil { // The layer may have been deleted between the call to `Map()` or // `Heads()` and the call to `Get()`, so we just ignore this error - if err == layer.ErrLayerDoesNotExist { + if errors.Is(err, layer.ErrLayerDoesNotExist) { continue } return nil, err diff --git a/image/store.go b/image/store.go index ccd8f00fd4..c457bc3a82 100644 --- a/image/store.go +++ b/image/store.go @@ -88,7 +88,7 @@ func (is *store) restore() error { } l, err = is.lss.Get(chainID) if err != nil { - if err == layer.ErrLayerDoesNotExist { + if errors.Is(err, layer.ErrLayerDoesNotExist) { logrus.WithFields(f{"chainID": chainID, "os": img.OperatingSystem(), "err": err}).Error("not restoring image") return nil } diff --git a/layer/layer_test.go b/layer/layer_test.go index 8eeb90f8df..41848576f9 100644 --- a/layer/layer_test.go +++ b/layer/layer_test.go @@ -2,6 +2,7 @@ package layer // import "github.com/docker/docker/layer" import ( "bytes" + "errors" "io" "os" "path/filepath" @@ -397,7 +398,7 @@ func TestStoreRestore(t *testing.T) { // Create again with same name, should return error if _, err := ls2.CreateRWLayer("some-mount_name", layer3b.ChainID(), nil); err == nil { t.Fatal("Expected error creating mount with same name") - } else if err != ErrMountNameConflict { + } else if !errors.Is(err, ErrMountNameConflict) { t.Fatal(err) }