Browse Source

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 <github@gone.nl>
Sebastiaan van Stijn 2 years ago
parent
commit
d131147a5c
3 changed files with 5 additions and 3 deletions
  1. 2 1
      daemon/images/image_list.go
  2. 1 1
      image/store.go
  3. 2 1
      layer/layer_test.go

+ 2 - 1
daemon/images/image_list.go

@@ -2,6 +2,7 @@ package images // import "github.com/docker/docker/daemon/images"
 
 
 import (
 import (
 	"context"
 	"context"
+	"errors"
 	"fmt"
 	"fmt"
 	"sort"
 	"sort"
 	"time"
 	"time"
@@ -132,7 +133,7 @@ func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions)
 			if err != nil {
 			if err != nil {
 				// The layer may have been deleted between the call to `Map()` or
 				// The layer may have been deleted between the call to `Map()` or
 				// `Heads()` and the call to `Get()`, so we just ignore this error
 				// `Heads()` and the call to `Get()`, so we just ignore this error
-				if err == layer.ErrLayerDoesNotExist {
+				if errors.Is(err, layer.ErrLayerDoesNotExist) {
 					continue
 					continue
 				}
 				}
 				return nil, err
 				return nil, err

+ 1 - 1
image/store.go

@@ -88,7 +88,7 @@ func (is *store) restore() error {
 			}
 			}
 			l, err = is.lss.Get(chainID)
 			l, err = is.lss.Get(chainID)
 			if err != nil {
 			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")
 					logrus.WithFields(f{"chainID": chainID, "os": img.OperatingSystem(), "err": err}).Error("not restoring image")
 					return nil
 					return nil
 				}
 				}

+ 2 - 1
layer/layer_test.go

@@ -2,6 +2,7 @@ package layer // import "github.com/docker/docker/layer"
 
 
 import (
 import (
 	"bytes"
 	"bytes"
+	"errors"
 	"io"
 	"io"
 	"os"
 	"os"
 	"path/filepath"
 	"path/filepath"
@@ -397,7 +398,7 @@ func TestStoreRestore(t *testing.T) {
 	// Create again with same name, should return error
 	// Create again with same name, should return error
 	if _, err := ls2.CreateRWLayer("some-mount_name", layer3b.ChainID(), nil); err == nil {
 	if _, err := ls2.CreateRWLayer("some-mount_name", layer3b.ChainID(), nil); err == nil {
 		t.Fatal("Expected error creating mount with same name")
 		t.Fatal("Expected error creating mount with same name")
-	} else if err != ErrMountNameConflict {
+	} else if !errors.Is(err, ErrMountNameConflict) {
 		t.Fatal(err)
 		t.Fatal(err)
 	}
 	}