mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Use errdefs for handling errors in client
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
053c6f097a
commit
0cafc84fb2
7 changed files with 12 additions and 12 deletions
|
@ -50,6 +50,7 @@ func (cli *Client) CopyToContainer(ctx context.Context, containerID, dstPath str
|
||||||
}
|
}
|
||||||
defer ensureReaderClosed(response)
|
defer ensureReaderClosed(response)
|
||||||
|
|
||||||
|
// TODO this code converts non-error status-codes (e.g., "204 No Content") into an error; verify if this is the desired behavior
|
||||||
if response.statusCode != http.StatusOK {
|
if response.statusCode != http.StatusOK {
|
||||||
return fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
|
return fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
|
||||||
}
|
}
|
||||||
|
@ -69,6 +70,7 @@ func (cli *Client) CopyFromContainer(ctx context.Context, containerID, srcPath s
|
||||||
return nil, types.ContainerPathStat{}, wrapResponseError(err, response, "container:path", containerID+":"+srcPath)
|
return nil, types.ContainerPathStat{}, wrapResponseError(err, response, "container:path", containerID+":"+srcPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO this code converts non-error status-codes (e.g., "204 No Content") into an error; verify if this is the desired behavior
|
||||||
if response.statusCode != http.StatusOK {
|
if response.statusCode != http.StatusOK {
|
||||||
return nil, types.ContainerPathStat{}, fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
|
return nil, types.ContainerPathStat{}, fmt.Errorf("unexpected status code from daemon: %d", response.statusCode)
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,6 +121,7 @@ func TestCopyToContainerNotFoundError(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO TestCopyToContainerNotStatusOKError expects a non-error status-code ("204 No Content") to produce an error; verify if this is the desired behavior
|
||||||
func TestCopyToContainerNotStatusOKError(t *testing.T) {
|
func TestCopyToContainerNotStatusOKError(t *testing.T) {
|
||||||
client := &Client{
|
client := &Client{
|
||||||
client: newMockClient(errorMock(http.StatusNoContent, "No content")),
|
client: newMockClient(errorMock(http.StatusNoContent, "No content")),
|
||||||
|
@ -200,6 +201,7 @@ func TestCopyFromContainerNotFoundError(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO TestCopyFromContainerNotStatusOKError expects a non-error status-code ("204 No Content") to produce an error; verify if this is the desired behavior
|
||||||
func TestCopyFromContainerNotStatusOKError(t *testing.T) {
|
func TestCopyFromContainerNotStatusOKError(t *testing.T) {
|
||||||
client := &Client{
|
client := &Client{
|
||||||
client: newMockClient(errorMock(http.StatusNoContent, "No content")),
|
client: newMockClient(errorMock(http.StatusNoContent, "No content")),
|
||||||
|
|
|
@ -3,12 +3,12 @@ package client // import "github.com/docker/docker/client"
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/docker/distribution/reference"
|
"github.com/docker/distribution/reference"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/errdefs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImagePull requests the docker host to pull an image from a remote registry.
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
|
resp, err := cli.tryImageCreate(ctx, query, options.RegistryAuth)
|
||||||
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
|
||||||
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
||||||
if privilegeErr != nil {
|
if privilegeErr != nil {
|
||||||
return nil, privilegeErr
|
return nil, privilegeErr
|
||||||
|
|
|
@ -4,11 +4,11 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/docker/distribution/reference"
|
"github.com/docker/distribution/reference"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/errdefs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImagePush requests the docker host to push an image to a remote registry.
|
// 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
|
||||||
query.Set("tag", tag)
|
query.Set("tag", tag)
|
||||||
|
|
||||||
resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
|
resp, err := cli.tryImagePush(ctx, name, query, options.RegistryAuth)
|
||||||
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
|
||||||
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
||||||
if privilegeErr != nil {
|
if privilegeErr != nil {
|
||||||
return nil, privilegeErr
|
return nil, privilegeErr
|
||||||
|
|
|
@ -4,12 +4,12 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
"github.com/docker/docker/api/types/filters"
|
"github.com/docker/docker/api/types/filters"
|
||||||
"github.com/docker/docker/api/types/registry"
|
"github.com/docker/docker/api/types/registry"
|
||||||
|
"github.com/docker/docker/errdefs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// ImageSearch makes the docker host to search by a term in a remote registry.
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := cli.tryImageSearch(ctx, query, options.RegistryAuth)
|
resp, err := cli.tryImageSearch(ctx, query, options.RegistryAuth)
|
||||||
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
|
||||||
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
||||||
if privilegeErr != nil {
|
if privilegeErr != nil {
|
||||||
return results, privilegeErr
|
return results, privilegeErr
|
||||||
|
|
|
@ -3,7 +3,6 @@ package client // import "github.com/docker/docker/client"
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
@ -15,9 +14,6 @@ import (
|
||||||
func (cli *Client) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registry.AuthenticateOKBody, error) {
|
func (cli *Client) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registry.AuthenticateOKBody, error) {
|
||||||
resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil)
|
resp, err := cli.post(ctx, "/auth", url.Values{}, auth, nil)
|
||||||
|
|
||||||
if resp.statusCode == http.StatusUnauthorized {
|
|
||||||
return registry.AuthenticateOKBody{}, unauthorizedError{err}
|
|
||||||
}
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return registry.AuthenticateOKBody{}, err
|
return registry.AuthenticateOKBody{}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,11 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
|
|
||||||
"github.com/docker/distribution/reference"
|
"github.com/docker/distribution/reference"
|
||||||
"github.com/docker/docker/api/types"
|
"github.com/docker/docker/api/types"
|
||||||
|
"github.com/docker/docker/errdefs"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ func (cli *Client) tryPluginPull(ctx context.Context, query url.Values, privileg
|
||||||
|
|
||||||
func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values, options types.PluginInstallOptions) (types.PluginPrivileges, error) {
|
func (cli *Client) checkPluginPermissions(ctx context.Context, query url.Values, options types.PluginInstallOptions) (types.PluginPrivileges, error) {
|
||||||
resp, err := cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
|
resp, err := cli.tryPluginPrivileges(ctx, query, options.RegistryAuth)
|
||||||
if resp.statusCode == http.StatusUnauthorized && options.PrivilegeFunc != nil {
|
if errdefs.IsUnauthorized(err) && options.PrivilegeFunc != nil {
|
||||||
// todo: do inspect before to check existing name before checking privileges
|
// todo: do inspect before to check existing name before checking privileges
|
||||||
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
newAuthHeader, privilegeErr := options.PrivilegeFunc()
|
||||||
if privilegeErr != nil {
|
if privilegeErr != nil {
|
||||||
|
|
Loading…
Reference in a new issue