Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 0cafc84

Browse files
committed
Use errdefs for handling errors in client
Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 053c6f0 commit 0cafc84

7 files changed

+12
-12
lines changed

client/container_copy.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (cli *Client) CopyToContainer(ctx context.Context, containerID, dstPath str
5050
}
5151
defer ensureReaderClosed(response)
5252

53+
// TODO this code converts non-error status-codes (e.g., "204 No Content") into an error; verify if this is the desired behavior
5354
if response.statusCode != http.StatusOK {
5455
return fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
5556
}
@@ -69,6 +70,7 @@ func (cli *Client) CopyFromContainer(ctx context.Context, containerID, srcPath s
6970
return nil, types.ContainerPathStat{}, wrapResponseError(err, response, "container:path", containerID+":"+srcPath)
7071
}
7172

73+
// TODO this code converts non-error status-codes (e.g., "204 No Content") into an error; verify if this is the desired behavior
7274
if response.statusCode != http.StatusOK {
7375
return nil, types.ContainerPathStat{}, fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
7476
}

client/container_copy_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func TestCopyToContainerNotFoundError(t *testing.T) {
121121
}
122122
}
123123

124+
// TODO TestCopyToContainerNotStatusOKError expects a non-error status-code ("204 No Content") to produce an error; verify if this is the desired behavior
124125
func TestCopyToContainerNotStatusOKError(t *testing.T) {
125126
client := &Client{
126127
client: newMockClient(errorMock(http.StatusNoContent, "No content")),
@@ -200,6 +201,7 @@ func TestCopyFromContainerNotFoundError(t *testing.T) {
200201
}
201202
}
202203

204+
// TODO TestCopyFromContainerNotStatusOKError expects a non-error status-code ("204 No Content") to produce an error; verify if this is the desired behavior
203205
func TestCopyFromContainerNotStatusOKError(t *testing.T) {
204206
client := &Client{
205207
client: newMockClient(errorMock(http.StatusNoContent, "No content")),

client/image_pull.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package client // import "github.com/docker/docker/client"
33
import (
44
"context"
55
"io"
6-
"net/http"
76
"net/url"
87
"strings"
98

109
"github.com/docker/distribution/reference"
1110
"github.com/docker/docker/api/types"
11+
"github.com/docker/docker/errdefs"
1212
)
1313

1414
// 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
3535
}
3636

3737
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 {
3939
newAuthHeader, privilegeErr := options.PrivilegeFunc()
4040
if privilegeErr != nil {
4141
return nil, privilegeErr

client/image_push.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"context"
55
"errors"
66
"io"
7-
"net/http"
87
"net/url"
98

109
"github.com/docker/distribution/reference"
1110
"github.com/docker/docker/api/types"
11+
"github.com/docker/docker/errdefs"
1212
)
1313

1414
// 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
3636
query.Set("tag", tag)
3737

3838
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 {
4040
newAuthHeader, privilegeErr := options.PrivilegeFunc()
4141
if privilegeErr != nil {
4242
return nil, privilegeErr

client/image_search.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
"net/http"
87
"net/url"
98

109
"github.com/docker/docker/api/types"
1110
"github.com/docker/docker/api/types/filters"
1211
"github.com/docker/docker/api/types/registry"
12+
"github.com/docker/docker/errdefs"
1313
)
1414

1515
// 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
2929
}
3030

3131
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 {
3333
newAuthHeader, privilegeErr := options.PrivilegeFunc()
3434
if privilegeErr != nil {
3535
return results, privilegeErr

client/login.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package client // import "github.com/docker/docker/client"
33
import (
44
"context"
55
"encoding/json"
6-
"net/http"
76
"net/url"
87

98
"github.com/docker/docker/api/types"
@@ -15,9 +14,6 @@ import (
1514
func (cli *Client) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registry.AuthenticateOKBody, error) {
1615
resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil)
1716

18-
if resp.statusCode == http.StatusUnauthorized {
19-
return registry.AuthenticateOKBody{}, unauthorizedError{err}
20-
}
2117
if err != nil {
2218
return registry.AuthenticateOKBody{}, err
2319
}

client/plugin_install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"context"
55
"encoding/json"
66
"io"
7-
"net/http"
87
"net/url"
98

109
"github.com/docker/distribution/reference"
1110
"github.com/docker/docker/api/types"
11+
"github.com/docker/docker/errdefs"
1212
"github.com/pkg/errors"
1313
)
1414

@@ -78,7 +78,7 @@ func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, privileg
7878

7979
func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values, options types.PluginInstallOptions) (types.PluginPrivileges, error) {
8080
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 {
8282
// todo: do inspect before to check existing name before checking privileges
8383
newAuthHeader, privilegeErr := options.PrivilegeFunc()
8484
if privilegeErr != nil {

0 commit comments

Comments
 (0)