Browse code

Use errdefs for handling errors in client

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2019/02/10 03:19:22
Showing 7 changed files
... ...
@@ -50,6 +50,7 @@ func (cli *Client) CopyToContainer(ctx context.Context, containerID, dstPath str
50 50
 	}
51 51
 	defer ensureReaderClosed(response)
52 52
 
53
+	// TODO this code converts non-error status-codes (e.g., "204 No Content") into an error; verify if this is the desired behavior
53 54
 	if response.statusCode != http.StatusOK {
54 55
 		return fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
55 56
 	}
... ...
@@ -69,6 +70,7 @@ func (cli *Client) CopyFromContainer(ctx context.Context, containerID, srcPath s
69 69
 		return nil, types.ContainerPathStat{}, wrapResponseError(err, response, "container:path", containerID+":"+srcPath)
70 70
 	}
71 71
 
72
+	// TODO this code converts non-error status-codes (e.g., "204 No Content") into an error; verify if this is the desired behavior
72 73
 	if response.statusCode != http.StatusOK {
73 74
 		return nil, types.ContainerPathStat{}, fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
74 75
 	}
... ...
@@ -121,6 +121,7 @@ func TestCopyToContainerNotFoundError(t *testing.T) {
121 121
 	}
122 122
 }
123 123
 
124
+// TODO TestCopyToContainerNotStatusOKError expects a non-error status-code ("204 No Content") to produce an error; verify if this is the desired behavior
124 125
 func TestCopyToContainerNotStatusOKError(t *testing.T) {
125 126
 	client := &Client{
126 127
 		client: newMockClient(errorMock(http.StatusNoContent, "No content")),
... ...
@@ -200,6 +201,7 @@ func TestCopyFromContainerNotFoundError(t *testing.T) {
200 200
 	}
201 201
 }
202 202
 
203
+// TODO TestCopyFromContainerNotStatusOKError expects a non-error status-code ("204 No Content") to produce an error; verify if this is the desired behavior
203 204
 func TestCopyFromContainerNotStatusOKError(t *testing.T) {
204 205
 	client := &Client{
205 206
 		client: newMockClient(errorMock(http.StatusNoContent, "No content")),
... ...
@@ -3,12 +3,12 @@ package client // import "github.com/docker/docker/client"
3 3
 import (
4 4
 	"context"
5 5
 	"io"
6
-	"net/http"
7 6
 	"net/url"
8 7
 	"strings"
9 8
 
10 9
 	"github.com/docker/distribution/reference"
11 10
 	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/errdefs"
12 12
 )
13 13
 
14 14
 // ImagePull requests the docker host to pull an image from a remote registry.
... ...
@@ -35,7 +35,7 @@ func (cli *Client) ImagePull(ctx context.Context, refStr string, options types.I
35 35
 	}
36 36
 
37 37
 	resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
38
-	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
38
+	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
39 39
 		newAuthHeader, privilegeErr := options.PrivilegeFunc()
40 40
 		if privilegeErr != nil {
41 41
 			return nil, privilegeErr
... ...
@@ -4,11 +4,11 @@ import (
4 4
 	"context"
5 5
 	"errors"
6 6
 	"io"
7
-	"net/http"
8 7
 	"net/url"
9 8
 
10 9
 	"github.com/docker/distribution/reference"
11 10
 	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/errdefs"
12 12
 )
13 13
 
14 14
 // ImagePush requests the docker host to push an image to a remote registry.
... ...
@@ -36,7 +36,7 @@ func (cli *Client) ImagePush(ctx context.Context, image string, options types.Im
36 36
 	query.Set("tag", tag)
37 37
 
38 38
 	resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
39
-	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
39
+	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
40 40
 		newAuthHeader, privilegeErr := options.PrivilegeFunc()
41 41
 		if privilegeErr != nil {
42 42
 			return nil, privilegeErr
... ...
@@ -4,12 +4,12 @@ import (
4 4
 	"context"
5 5
 	"encoding/json"
6 6
 	"fmt"
7
-	"net/http"
8 7
 	"net/url"
9 8
 
10 9
 	"github.com/docker/docker/api/types"
11 10
 	"github.com/docker/docker/api/types/filters"
12 11
 	"github.com/docker/docker/api/types/registry"
12
+	"github.com/docker/docker/errdefs"
13 13
 )
14 14
 
15 15
 // ImageSearch makes the docker host to search by a term in a remote registry.
... ...
@@ -29,7 +29,7 @@ func (cli *Client) ImageSearch(ctx context.Context, term string, options types.I
29 29
 	}
30 30
 
31 31
 	resp, err := cli.tryImageSearch(ctx, query, options.RegistryAuth)
32
-	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
32
+	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
33 33
 		newAuthHeader, privilegeErr := options.PrivilegeFunc()
34 34
 		if privilegeErr != nil {
35 35
 			return results, privilegeErr
... ...
@@ -3,7 +3,6 @@ package client // import "github.com/docker/docker/client"
3 3
 import (
4 4
 	"context"
5 5
 	"encoding/json"
6
-	"net/http"
7 6
 	"net/url"
8 7
 
9 8
 	"github.com/docker/docker/api/types"
... ...
@@ -15,9 +14,6 @@ import (
15 15
 func (cli *Client) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registry.AuthenticateOKBody, error) {
16 16
 	resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil)
17 17
 
18
-	if resp.statusCode == http.StatusUnauthorized {
19
-		return registry.AuthenticateOKBody{}, unauthorizedError{err}
20
-	}
21 18
 	if err != nil {
22 19
 		return registry.AuthenticateOKBody{}, err
23 20
 	}
... ...
@@ -4,11 +4,11 @@ import (
4 4
 	"context"
5 5
 	"encoding/json"
6 6
 	"io"
7
-	"net/http"
8 7
 	"net/url"
9 8
 
10 9
 	"github.com/docker/distribution/reference"
11 10
 	"github.com/docker/docker/api/types"
11
+	"github.com/docker/docker/errdefs"
12 12
 	"github.com/pkg/errors"
13 13
 )
14 14
 
... ...
@@ -78,7 +78,7 @@ func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, privileg
78 78
 
79 79
 func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values, options types.PluginInstallOptions) (types.PluginPrivileges, error) {
80 80
 	resp, err := cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
81
-	if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
81
+	if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
82 82
 		// todo: do inspect before to check existing name before checking privileges
83 83
 		newAuthHeader, privilegeErr := options.PrivilegeFunc()
84 84
 		if privilegeErr != nil {