Browse Source

Merge pull request #34528 from adshmh/client-should-return-image-not-found-for-404-status

client to return imageNotFound error if API returns 404 status code
Yong Tang 7 years ago
parent
commit
db73f3daee
2 changed files with 15 additions and 0 deletions
  1. 4 0
      client/image_remove.go
  2. 11 0
      client/image_remove_test.go

+ 4 - 0
client/image_remove.go

@@ -2,6 +2,7 @@ package client
 
 import (
 	"encoding/json"
+	"net/http"
 	"net/url"
 
 	"github.com/docker/docker/api/types"
@@ -21,6 +22,9 @@ func (cli *Client) ImageRemove(ctx context.Context, imageID string, options type
 
 	resp, err := cli.delete(ctx, "/images/"+imageID, query, nil)
 	if err != nil {
+		if resp.statusCode == http.StatusNotFound {
+			return nil, imageNotFoundError{imageID}
+		}
 		return nil, err
 	}
 

+ 11 - 0
client/image_remove_test.go

@@ -24,6 +24,17 @@ func TestImageRemoveError(t *testing.T) {
 	}
 }
 
+func TestImageRemoveImageNotFound(t *testing.T) {
+	client := &Client{
+		client: newMockClient(errorMock(http.StatusNotFound, "Server error")),
+	}
+
+	_, err := client.ImageRemove(context.Background(), "unknown", types.ImageRemoveOptions{})
+	if err == nil || !IsErrNotFound(err) {
+		t.Fatalf("expected an imageNotFoundError error, got %v", err)
+	}
+}
+
 func TestImageRemove(t *testing.T) {
 	expectedURL := "/images/image_id"
 	removeCases := []struct {