Skip to content

Commit 804ae89

Browse files
committed
errors: use errdefs errors in client and commands
This change moves from specific, global errors to the errdefs errors. This makes it easy to handle certain classes of errors while still adding context to the failure. Signed-off-by: Stephen Day <[email protected]>
1 parent d6be45e commit 804ae89

8 files changed

Lines changed: 20 additions & 111 deletions

File tree

client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
137137
c.conn, c.connector = conn, connector
138138
}
139139
if copts.services == nil && c.conn == nil {
140-
return nil, ErrNoGRPCAndService
140+
return nil, errors.Wrap(errdefs.ErrUnavailable, "no grpc connection or services is available")
141141
}
142142

143143
// check namespace labels for default runtime
@@ -196,7 +196,7 @@ type Client struct {
196196
// Reconnect re-establishes the GRPC connection to the containerd daemon
197197
func (c *Client) Reconnect() error {
198198
if c.connector == nil {
199-
return ErrReconnectFailed
199+
return errors.Wrap(errdefs.ErrUnavailable, "unable to reconnect to containerd, no connector available")
200200
}
201201
c.connMu.Lock()
202202
defer c.connMu.Unlock()
@@ -219,7 +219,7 @@ func (c *Client) IsServing(ctx context.Context) (bool, error) {
219219
c.connMu.Lock()
220220
if c.conn == nil {
221221
c.connMu.Unlock()
222-
return false, ErrNoGRPC
222+
return false, errors.Wrap(errdefs.ErrUnavailable, "no grpc connection available")
223223
}
224224
c.connMu.Unlock()
225225
r, err := c.HealthService().Check(ctx, &grpc_health_v1.HealthCheckRequest{}, grpc.WaitForReady(true))
@@ -350,7 +350,7 @@ func (c *Client) Fetch(ctx context.Context, ref string, opts ...RemoteOpt) (imag
350350
}
351351

352352
if fetchCtx.Unpack {
353-
return images.Image{}, ErrUnpackNotSupported
353+
return images.Image{}, errors.Wrap(errdefs.ErrNotImplemented, "unpack on fetch not supported, try pull")
354354
}
355355

356356
if fetchCtx.PlatformMatcher == nil {
@@ -656,7 +656,7 @@ func (c *Client) Version(ctx context.Context) (Version, error) {
656656
c.connMu.Lock()
657657
if c.conn == nil {
658658
c.connMu.Unlock()
659-
return Version{}, ErrNoGRPC
659+
return Version{}, errors.Wrap(errdefs.ErrUnavailable, "no grpc connection available")
660660
}
661661
c.connMu.Unlock()
662662
response, err := c.VersionService().Version(ctx, &ptypes.Empty{})

cmd/containerd/command/error.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

cmd/containerd/command/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"runtime"
2828
"time"
2929

30+
"github.com/containerd/containerd/errdefs"
3031
"github.com/containerd/containerd/log"
3132
"github.com/containerd/containerd/mount"
3233
"github.com/containerd/containerd/services/server"
@@ -152,7 +153,7 @@ func App() *cli.App {
152153
ttrpcAddress = fmt.Sprintf("%s.ttrpc", config.GRPC.Address)
153154
)
154155
if address == "" {
155-
return ErrEmptyGRCPAddress
156+
return errors.Wrap(errdefs.ErrInvalidArgument, "grpc address cannot be empty")
156157
}
157158
log.G(ctx).WithFields(logrus.Fields{
158159
"version": version.Version,

cmd/containerd/command/publish.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ var publishCommand = cli.Command{
5151
ctx := namespaces.WithNamespace(gocontext.Background(), context.String("namespace"))
5252
topic := context.String("topic")
5353
if topic == "" {
54-
return ErrEmptyTopic
54+
return errors.Wrap(errdefs.ErrInvalidArgument, "topic required to publish event")
5555
}
5656
payload, err := getEventPayload(os.Stdin)
5757
if err != nil {

cmd/containerd/command/service_windows.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import (
2727
"time"
2828
"unsafe"
2929

30+
"github.com/containerd/containerd/errdefs"
3031
"github.com/containerd/containerd/services/server"
32+
"github.com/pkg/errors"
3133
"github.com/sirupsen/logrus"
3234
"github.com/urfave/cli"
3335
"golang.org/x/sys/windows"
@@ -161,7 +163,7 @@ func (h *etwHook) Fire(e *logrus.Entry) error {
161163
etype = windows.EVENTLOG_INFORMATION_TYPE
162164
eid = eventDebug
163165
default:
164-
return ErrUnknownLevel
166+
return errors.Wrap(errdefs.ErrInvalidArgument, "unknown level")
165167
}
166168

167169
// If there is additional data, include it as a second string.
@@ -310,7 +312,7 @@ func registerUnregisterService(root string) (bool, error) {
310312

311313
if unregisterServiceFlag {
312314
if registerServiceFlag {
313-
return true, ErrRegisterAndUnregisterService
315+
return true, errors.Wrap(errdefs.ErrInvalidArgument, "--register-service and --unregister-service cannot be used together")
314316
}
315317
return true, unregisterService()
316318
}

cmd/ctr/commands/containers/containers.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ import (
2828
"github.com/containerd/containerd/cmd/ctr/commands"
2929
"github.com/containerd/containerd/cmd/ctr/commands/run"
3030
"github.com/containerd/containerd/containers"
31+
"github.com/containerd/containerd/errdefs"
3132
"github.com/containerd/containerd/log"
3233
"github.com/containerd/typeurl"
34+
"github.com/pkg/errors"
3335
"github.com/urfave/cli"
3436
)
3537

@@ -64,17 +66,17 @@ var createCommand = cli.Command{
6466
if config {
6567
id = context.Args().First()
6668
if context.NArg() > 1 {
67-
return commands.ErrArgConfigFile
69+
return errors.Wrap(errdefs.ErrInvalidArgument, "with spec config file, only container id should be provided")
6870
}
6971
} else {
7072
id = context.Args().Get(1)
7173
ref = context.Args().First()
7274
if ref == "" {
73-
return commands.ErrUnprovidedImageRef
75+
return errors.Wrap(errdefs.ErrInvalidArgument, "image ref must be provided")
7476
}
7577
}
7678
if id == "" {
77-
return commands.ErrEmptyContainerID
79+
return errors.Wrap(errdefs.ErrInvalidArgument, "container id must be provided")
7880
}
7981
client, ctx, cancel, err := commands.NewClient(context)
8082
if err != nil {
@@ -167,7 +169,7 @@ var deleteCommand = cli.Command{
167169
}
168170

169171
if context.NArg() == 0 {
170-
return commands.ErrDeleteNoneContainer
172+
return errors.Wrap(errdefs.ErrInvalidArgument, "must specify at least one container to delete")
171173
}
172174
for _, arg := range context.Args() {
173175
if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil {
@@ -213,7 +215,7 @@ var setLabelsCommand = cli.Command{
213215
Action: func(context *cli.Context) error {
214216
containerID, labels := commands.ObjectWithLabelArgs(context)
215217
if containerID == "" {
216-
return commands.ErrEmptyContainerID
218+
return errors.Wrap(errdefs.ErrInvalidArgument, "container id must be provided")
217219
}
218220
client, ctx, cancel, err := commands.NewClient(context)
219221
if err != nil {
@@ -249,7 +251,7 @@ var infoCommand = cli.Command{
249251
Action: func(context *cli.Context) error {
250252
id := context.Args().First()
251253
if id == "" {
252-
return commands.ErrEmptyContainerID
254+
return errors.Wrap(errdefs.ErrInvalidArgument, "container id must be provided")
253255
}
254256
client, ctx, cancel, err := commands.NewClient(context)
255257
if err != nil {

cmd/ctr/commands/error.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

error.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)