Skip to content

Commit bfc6aec

Browse files
committed
cli/command/container: define local errors instead of errdefs
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
1 parent eee0cf9 commit bfc6aec

7 files changed

Lines changed: 48 additions & 19 deletions

File tree

cli/command/container/create.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/docker/docker/api/types/mount"
3030
"github.com/docker/docker/api/types/versions"
3131
"github.com/docker/docker/client"
32-
"github.com/docker/docker/errdefs"
3332
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3433
"github.com/pkg/errors"
3534
"github.com/spf13/cobra"
@@ -317,7 +316,7 @@ func createContainer(ctx context.Context, dockerCli command.Cli, containerCfg *c
317316
if options.platform != "" && versions.GreaterThanOrEqualTo(dockerCli.Client().ClientVersion(), "1.41") {
318317
p, err := platforms.Parse(options.platform)
319318
if err != nil {
320-
return "", errors.Wrap(errdefs.InvalidParameter(err), "error parsing specified platform")
319+
return "", errors.Wrap(invalidParameter(err), "error parsing specified platform")
321320
}
322321
platform = &p
323322
}

cli/command/container/errors.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package container
2+
3+
import cerrdefs "github.com/containerd/errdefs"
4+
5+
func invalidParameter(err error) error {
6+
if err == nil || cerrdefs.IsInvalidArgument(err) {
7+
return err
8+
}
9+
return invalidParameterErr{err}
10+
}
11+
12+
type invalidParameterErr struct{ error }
13+
14+
func (invalidParameterErr) InvalidParameter() {}
15+
func (e invalidParameterErr) Unwrap() error {
16+
return e.error
17+
}
18+
19+
func notFound(err error) error {
20+
if err == nil || cerrdefs.IsNotFound(err) {
21+
return err
22+
}
23+
return notFoundErr{err}
24+
}
25+
26+
type notFoundErr struct{ error }
27+
28+
func (notFoundErr) NotFound() {}
29+
func (e notFoundErr) Unwrap() error {
30+
return e.error
31+
}

cli/command/container/opts.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
mounttypes "github.com/docker/docker/api/types/mount"
2020
networktypes "github.com/docker/docker/api/types/network"
2121
"github.com/docker/docker/api/types/strslice"
22-
"github.com/docker/docker/errdefs"
2322
"github.com/docker/go-connections/nat"
2423
"github.com/pkg/errors"
2524
"github.com/spf13/pflag"
@@ -781,7 +780,7 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*networktypes.Endpoin
781780
return nil, err
782781
}
783782
if _, ok := endpoints[n.Target]; ok {
784-
return nil, errdefs.InvalidParameter(errors.Errorf("network %q is specified multiple times", n.Target))
783+
return nil, invalidParameter(errors.Errorf("network %q is specified multiple times", n.Target))
785784
}
786785

787786
// For backward compatibility: if no custom options are provided for the network,
@@ -795,30 +794,30 @@ func parseNetworkOpts(copts *containerOptions) (map[string]*networktypes.Endpoin
795794
endpoints[n.Target] = ep
796795
}
797796
if hasUserDefined && hasNonUserDefined {
798-
return nil, errdefs.InvalidParameter(errors.New("conflicting options: cannot attach both user-defined and non-user-defined network-modes"))
797+
return nil, invalidParameter(errors.New("conflicting options: cannot attach both user-defined and non-user-defined network-modes"))
799798
}
800799
return endpoints, nil
801800
}
802801

803802
func applyContainerOptions(n *opts.NetworkAttachmentOpts, copts *containerOptions) error { //nolint:gocyclo
804803
// TODO should we error if _any_ advanced option is used? (i.e. forbid to combine advanced notation with the "old" flags (`--network-alias`, `--link`, `--ip`, `--ip6`)?
805804
if len(n.Aliases) > 0 && copts.aliases.Len() > 0 {
806-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --network-alias and per-network alias"))
805+
return invalidParameter(errors.New("conflicting options: cannot specify both --network-alias and per-network alias"))
807806
}
808807
if len(n.Links) > 0 && copts.links.Len() > 0 {
809-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --link and per-network links"))
808+
return invalidParameter(errors.New("conflicting options: cannot specify both --link and per-network links"))
810809
}
811810
if n.IPv4Address != "" && copts.ipv4Address != "" {
812-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --ip and per-network IPv4 address"))
811+
return invalidParameter(errors.New("conflicting options: cannot specify both --ip and per-network IPv4 address"))
813812
}
814813
if n.IPv6Address != "" && copts.ipv6Address != "" {
815-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --ip6 and per-network IPv6 address"))
814+
return invalidParameter(errors.New("conflicting options: cannot specify both --ip6 and per-network IPv6 address"))
816815
}
817816
if n.MacAddress != "" && copts.macAddress != "" {
818-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --mac-address and per-network MAC address"))
817+
return invalidParameter(errors.New("conflicting options: cannot specify both --mac-address and per-network MAC address"))
819818
}
820819
if len(n.LinkLocalIPs) > 0 && copts.linkLocalIPs.Len() > 0 {
821-
return errdefs.InvalidParameter(errors.New("conflicting options: cannot specify both --link-local-ip and per-network link-local IP addresses"))
820+
return invalidParameter(errors.New("conflicting options: cannot specify both --link-local-ip and per-network link-local IP addresses"))
822821
}
823822
if copts.aliases.Len() > 0 {
824823
n.Aliases = make([]string, copts.aliases.Len())

cli/command/container/prune.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/docker/cli/cli/command/completion"
1010
"github.com/docker/cli/internal/prompt"
1111
"github.com/docker/cli/opts"
12-
"github.com/docker/docker/errdefs"
1312
units "github.com/docker/go-units"
1413
"github.com/pkg/errors"
1514
"github.com/spf13/cobra"
@@ -62,7 +61,7 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
6261
return 0, "", err
6362
}
6463
if !r {
65-
return 0, "", errdefs.Cancelled(errors.New("container prune has been cancelled"))
64+
return 0, "", cancelledErr{errors.New("container prune has been cancelled")}
6665
}
6766
}
6867

@@ -82,6 +81,10 @@ func runPrune(ctx context.Context, dockerCli command.Cli, options pruneOptions)
8281
return spaceReclaimed, output, nil
8382
}
8483

84+
type cancelledErr struct{ error }
85+
86+
func (cancelledErr) Cancelled() {}
87+
8588
// RunPrune calls the Container Prune API
8689
// This returns the amount of space reclaimed and a detailed output string
8790
func RunPrune(ctx context.Context, dockerCli command.Cli, _ bool, filter opts.FilterOpt) (uint64, string, error) {

cli/command/container/restart_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/docker/cli/internal/test"
1212
"github.com/docker/docker/api/types/container"
13-
"github.com/docker/docker/errdefs"
1413
"gotest.tools/v3/assert"
1514
is "gotest.tools/v3/assert/cmp"
1615
)
@@ -66,7 +65,7 @@ func TestRestart(t *testing.T) {
6665
containerRestartFunc: func(ctx context.Context, containerID string, options container.StopOptions) error {
6766
assert.Check(t, is.DeepEqual(options, tc.expectedOpts))
6867
if containerID == "nosuchcontainer" {
69-
return errdefs.NotFound(errors.New("Error: no such container: " + containerID))
68+
return notFound(errors.New("Error: no such container: " + containerID))
7069
}
7170

7271
// TODO(thaJeztah): consider using parallelOperation for restart, similar to "stop" and "remove"

cli/command/container/rm_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/docker/cli/internal/test"
1212
"github.com/docker/docker/api/types/container"
13-
"github.com/docker/docker/errdefs"
1413
"gotest.tools/v3/assert"
1514
)
1615

@@ -36,7 +35,7 @@ func TestRemoveForce(t *testing.T) {
3635
mutex.Unlock()
3736

3837
if container == "nosuchcontainer" {
39-
return errdefs.NotFound(errors.New("Error: no such container: " + container))
38+
return notFound(errors.New("Error: no such container: " + container))
4039
}
4140
return nil
4241
},

cli/command/container/stop_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010

1111
"github.com/docker/cli/internal/test"
1212
"github.com/docker/docker/api/types/container"
13-
"github.com/docker/docker/errdefs"
1413
"gotest.tools/v3/assert"
1514
is "gotest.tools/v3/assert/cmp"
1615
)
@@ -66,7 +65,7 @@ func TestStop(t *testing.T) {
6665
containerStopFunc: func(ctx context.Context, containerID string, options container.StopOptions) error {
6766
assert.Check(t, is.DeepEqual(options, tc.expectedOpts))
6867
if containerID == "nosuchcontainer" {
69-
return errdefs.NotFound(errors.New("Error: no such container: " + containerID))
68+
return notFound(errors.New("Error: no such container: " + containerID))
7069
}
7170

7271
// containerStopFunc is called in parallel for each container

0 commit comments

Comments
 (0)