Skip to content

Commit 61d930a

Browse files
committed
Move specific errors to their respective packages
Signed-off-by: Michael Crosby <[email protected]>
1 parent abc152d commit 61d930a

9 files changed

Lines changed: 118 additions & 37 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, errdefs.ErrNoGRPCAndService
140+
return nil, ErrNoGRPCAndService
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 errdefs.ErrReconnectFailed
199+
return ErrReconnectFailed
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, errdefs.ErrNoGRPC
222+
return false, ErrNoGRPC
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{}, errdefs.ErrUnpackNotSupported
353+
return images.Image{}, ErrUnpackNotSupported
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{}, errdefs.ErrNoGRPC
659+
return Version{}, ErrNoGRPC
660660
}
661661
c.connMu.Unlock()
662662
response, err := c.VersionService().Version(ctx, &ptypes.Empty{})

cmd/containerd/command/error.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package command
18+
19+
import (
20+
"github.com/pkg/errors"
21+
)
22+
23+
var (
24+
// ErrUnknownLevel is returned when an unknown debugging level is encountered
25+
ErrUnknownLevel = errors.New("unknown level")
26+
// ErrRegisterAndUnregisterService is returned when both register and unregister flags are specified
27+
ErrRegisterAndUnregisterService = errors.New("--register-service and --unregister-service cannot be used together")
28+
// ErrEmptyTopic is returned when no topic is provided
29+
ErrEmptyTopic = errors.New("topic required to publish event")
30+
// ErrEmptyGRCPAddress is returned when the grpc address is empty
31+
ErrEmptyGRCPAddress = errors.New("grpc address cannot be empty")
32+
)

cmd/containerd/command/main.go

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

30-
"github.com/containerd/containerd/errdefs"
3130
"github.com/containerd/containerd/log"
3231
"github.com/containerd/containerd/mount"
3332
"github.com/containerd/containerd/services/server"
@@ -153,7 +152,7 @@ func App() *cli.App {
153152
ttrpcAddress = fmt.Sprintf("%s.ttrpc", config.GRPC.Address)
154153
)
155154
if address == "" {
156-
return errdefs.ErrEmptyGRCPAddress
155+
return ErrEmptyGRCPAddress
157156
}
158157
log.G(ctx).WithFields(logrus.Fields{
159158
"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 errdefs.ErrEmptyTopic
54+
return ErrEmptyTopic
5555
}
5656
payload, err := getEventPayload(os.Stdin)
5757
if err != nil {

cmd/containerd/command/service_windows.go

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

30-
"github.com/containerd/containerd/errdefs"
3130
"github.com/containerd/containerd/services/server"
3231
"github.com/sirupsen/logrus"
3332
"github.com/urfave/cli"
@@ -162,7 +161,7 @@ func (h *etwHook) Fire(e *logrus.Entry) error {
162161
etype = windows.EVENTLOG_INFORMATION_TYPE
163162
eid = eventDebug
164163
default:
165-
return errdefs.ErrUnknownLevel
164+
return ErrUnknownLevel
166165
}
167166

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

312311
if unregisterServiceFlag {
313312
if registerServiceFlag {
314-
return true, errdefs.ErrRegisterAndUnregisterService
313+
return true, ErrRegisterAndUnregisterService
315314
}
316315
return true, unregisterService()
317316
}

cmd/ctr/commands/containers/containers.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ 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"
3231
"github.com/containerd/containerd/log"
3332
"github.com/containerd/typeurl"
3433
"github.com/urfave/cli"
@@ -65,17 +64,17 @@ var createCommand = cli.Command{
6564
if config {
6665
id = context.Args().First()
6766
if context.NArg() > 1 {
68-
return errdefs.ErrArgConfigFile
67+
return commands.ErrArgConfigFile
6968
}
7069
} else {
7170
id = context.Args().Get(1)
7271
ref = context.Args().First()
7372
if ref == "" {
74-
return errdefs.ErrUnprovidedImageRef
73+
return commands.ErrUnprovidedImageRef
7574
}
7675
}
7776
if id == "" {
78-
return errdefs.ErrEmptyContainerID
77+
return commands.ErrEmptyContainerID
7978
}
8079
client, ctx, cancel, err := commands.NewClient(context)
8180
if err != nil {
@@ -168,7 +167,7 @@ var deleteCommand = cli.Command{
168167
}
169168

170169
if context.NArg() == 0 {
171-
return errdefs.ErrDeleteNoneContainer
170+
return commands.ErrDeleteNoneContainer
172171
}
173172
for _, arg := range context.Args() {
174173
if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil {
@@ -214,7 +213,7 @@ var setLabelsCommand = cli.Command{
214213
Action: func(context *cli.Context) error {
215214
containerID, labels := commands.ObjectWithLabelArgs(context)
216215
if containerID == "" {
217-
return errdefs.ErrEmptyContainerID
216+
return commands.ErrEmptyContainerID
218217
}
219218
client, ctx, cancel, err := commands.NewClient(context)
220219
if err != nil {
@@ -250,7 +249,7 @@ var infoCommand = cli.Command{
250249
Action: func(context *cli.Context) error {
251250
id := context.Args().First()
252251
if id == "" {
253-
return errdefs.ErrEmptyContainerID
252+
return commands.ErrEmptyContainerID
254253
}
255254
client, ctx, cancel, err := commands.NewClient(context)
256255
if err != nil {

cmd/ctr/commands/error.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package commands
18+
19+
import (
20+
"github.com/pkg/errors"
21+
)
22+
23+
var (
24+
// ErrArgConfigFile is returned when the configuration for a spec is provided
25+
ErrArgConfigFile = errors.New("with spec config file, only container id should be provided")
26+
// ErrUnprovidedImageRef is returned when no image reference is provided
27+
ErrUnprovidedImageRef = errors.New("image ref must be provided")
28+
// ErrEmptyContainerID is returned when no container id is provided
29+
ErrEmptyContainerID = errors.New("container id must be provided")
30+
// ErrDeleteNoneContainer is returned when no container ids are provided for deletion
31+
ErrDeleteNoneContainer = errors.New("must specify at least one container to delete")
32+
)

errdefs/errors.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,13 @@ import (
4040
// For the most part, we just try to provide local grpc errors. Most conditions
4141
// map very well to those defined by grpc.
4242
var (
43-
ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping.
44-
ErrInvalidArgument = errors.New("invalid argument")
45-
ErrNotFound = errors.New("not found")
46-
ErrAlreadyExists = errors.New("already exists")
47-
ErrFailedPrecondition = errors.New("failed precondition")
48-
ErrUnavailable = errors.New("unavailable")
49-
ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented
50-
ErrNoGRPCAndService = errors.New("no grpc connection and services is available")
51-
ErrNoGRPC = errors.New("no grpc connection available")
52-
ErrReconnectFailed = errors.New("unable to reconnect to containerd, no connector available")
53-
ErrUnpackNotSupported = errors.New("unpack on fetch not supported, try pull")
54-
ErrEmptyGRCPAddress = errors.New("grpc address cannot be empty")
55-
ErrEmptyTopic = errors.New("topic required to publish event")
56-
ErrUnknownLevel = errors.New("unknown level")
57-
ErrRegisterAndUnregisterService = errors.New("--register-service and --unregister-service cannot be used together")
58-
ErrArgConfigFile = errors.New("with spec config file, only container id should be provided")
59-
ErrUnprovidedImageRef = errors.New("image ref must be provided")
60-
ErrEmptyContainerID = errors.New("container id must be provided")
61-
ErrDeleteNoneContainer = errors.New("must specify at least one container to delete")
43+
ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping.
44+
ErrInvalidArgument = errors.New("invalid argument")
45+
ErrNotFound = errors.New("not found")
46+
ErrAlreadyExists = errors.New("already exists")
47+
ErrFailedPrecondition = errors.New("failed precondition")
48+
ErrUnavailable = errors.New("unavailable")
49+
ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented
6250
)
6351

6452
// IsInvalidArgument returns true if the error is due to an invalid argument

error.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package containerd
18+
19+
import (
20+
"github.com/pkg/errors"
21+
)
22+
23+
var (
24+
// ErrNoGRPCAndService is returned when no connection or service is available
25+
ErrNoGRPCAndService = errors.New("no grpc connection and service is available")
26+
// ErrReconnectFailed is returned when no connector is available to reconnect
27+
ErrReconnectFailed = errors.New("unable to reconnect to containerd, no connector available")
28+
// ErrNoGRPC is returned when no grpc connect is available
29+
ErrNoGRPC = errors.New("no grpc connection available")
30+
// ErrUnpackNotSupported is returned when a fetch cannot unpack
31+
ErrUnpackNotSupported = errors.New("unpack on fetch not supported, try pull")
32+
)

0 commit comments

Comments
 (0)