Skip to content

Commit d6be45e

Browse files
authored
Merge pull request #3416 from crosbymichael/hard-code-err
Replace hard coded error messages
2 parents 7d03fc6 + 61d930a commit d6be45e

8 files changed

Lines changed: 111 additions & 17 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, errors.New("no grpc connection or services is available")
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 errors.New("unable to reconnect to containerd, no connector available")
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, errors.New("no grpc connection available")
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{}, errors.New("unpack on fetch not supported, try pull")
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{}, errors.New("no grpc connection available")
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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func App() *cli.App {
152152
ttrpcAddress = fmt.Sprintf("%s.ttrpc", config.GRPC.Address)
153153
)
154154
if address == "" {
155-
return errors.New("grpc address cannot be empty")
155+
return ErrEmptyGRCPAddress
156156
}
157157
log.G(ctx).WithFields(logrus.Fields{
158158
"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 errors.New("topic required to publish event")
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
@@ -18,7 +18,6 @@ package command
1818

1919
import (
2020
"bytes"
21-
"errors"
2221
"fmt"
2322
"io/ioutil"
2423
"log"
@@ -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 errors.New("unknown level")
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, errors.New("--register-service and --unregister-service cannot be used together")
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
@@ -30,7 +30,6 @@ import (
3030
"github.com/containerd/containerd/containers"
3131
"github.com/containerd/containerd/log"
3232
"github.com/containerd/typeurl"
33-
"github.com/pkg/errors"
3433
"github.com/urfave/cli"
3534
)
3635

@@ -65,17 +64,17 @@ var createCommand = cli.Command{
6564
if config {
6665
id = context.Args().First()
6766
if context.NArg() > 1 {
68-
return errors.New("with spec config file, only container id should be provided")
67+
return commands.ErrArgConfigFile
6968
}
7069
} else {
7170
id = context.Args().Get(1)
7271
ref = context.Args().First()
7372
if ref == "" {
74-
return errors.New("image ref must be provided")
73+
return commands.ErrUnprovidedImageRef
7574
}
7675
}
7776
if id == "" {
78-
return errors.New("container id must be provided")
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 errors.New("must specify at least one container to delete")
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 errors.New("container id must be provided")
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 errors.New("container id must be provided")
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+
)

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)