Skip to content

Commit db95af4

Browse files
committed
centralize harded-code message
Signed-off-by: Fahed Dorgaa <[email protected]>
1 parent f2b6c31 commit db95af4

6 files changed

Lines changed: 37 additions & 24 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, errdefs.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 errdefs.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, errdefs.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{}, errdefs.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{}, errdefs.ErrNoGRPC
660660
}
661661
c.connMu.Unlock()
662662
response, err := c.VersionService().Version(ctx, &ptypes.Empty{})

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 errors.New("grpc address cannot be empty")
156+
return errdefs.ErrEmptyGRCPAddress
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 errors.New("topic required to publish event")
54+
return errdefs.ErrEmptyTopic
5555
}
5656
payload, err := getEventPayload(os.Stdin)
5757
if err != nil {

cmd/containerd/command/service_windows.go

Lines changed: 3 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"
@@ -28,6 +27,7 @@ import (
2827
"time"
2928
"unsafe"
3029

30+
"github.com/containerd/containerd/errdefs"
3131
"github.com/containerd/containerd/services/server"
3232
"github.com/sirupsen/logrus"
3333
"github.com/urfave/cli"
@@ -162,7 +162,7 @@ func (h *etwHook) Fire(e *logrus.Entry) error {
162162
etype = windows.EVENTLOG_INFORMATION_TYPE
163163
eid = eventDebug
164164
default:
165-
return errors.New("unknown level")
165+
return errdefs.ErrUnknownLevel
166166
}
167167

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

312312
if unregisterServiceFlag {
313313
if registerServiceFlag {
314-
return true, errors.New("--register-service and --unregister-service cannot be used together")
314+
return true, errdefs.ErrRegisterAndUnregisterService
315315
}
316316
return true, unregisterService()
317317
}

cmd/ctr/commands/containers/containers.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ 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"
33-
"github.com/pkg/errors"
3434
"github.com/urfave/cli"
3535
)
3636

@@ -65,17 +65,17 @@ var createCommand = cli.Command{
6565
if config {
6666
id = context.Args().First()
6767
if context.NArg() > 1 {
68-
return errors.New("with spec config file, only container id should be provided")
68+
return errdefs.ErrArgConfigFile
6969
}
7070
} else {
7171
id = context.Args().Get(1)
7272
ref = context.Args().First()
7373
if ref == "" {
74-
return errors.New("image ref must be provided")
74+
return errdefs.ErrUnprovidedImageRef
7575
}
7676
}
7777
if id == "" {
78-
return errors.New("container id must be provided")
78+
return errdefs.ErrEmptyContainerId
7979
}
8080
client, ctx, cancel, err := commands.NewClient(context)
8181
if err != nil {
@@ -168,7 +168,7 @@ var deleteCommand = cli.Command{
168168
}
169169

170170
if context.NArg() == 0 {
171-
return errors.New("must specify at least one container to delete")
171+
return errdefs.ErrDeleteNoneContainer
172172
}
173173
for _, arg := range context.Args() {
174174
if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil {
@@ -214,7 +214,7 @@ var setLabelsCommand = cli.Command{
214214
Action: func(context *cli.Context) error {
215215
containerID, labels := commands.ObjectWithLabelArgs(context)
216216
if containerID == "" {
217-
return errors.New("container id must be provided")
217+
return errdefs.ErrEmptyContainerId
218218
}
219219
client, ctx, cancel, err := commands.NewClient(context)
220220
if err != nil {
@@ -250,7 +250,7 @@ var infoCommand = cli.Command{
250250
Action: func(context *cli.Context) error {
251251
id := context.Args().First()
252252
if id == "" {
253-
return errors.New("container id must be provided")
253+
return errdefs.ErrEmptyContainerId
254254
}
255255
client, ctx, cancel, err := commands.NewClient(context)
256256
if err != nil {

errdefs/errors.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,25 @@ 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
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")
5062
)
5163

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

0 commit comments

Comments
 (0)