centralize harded-code message

Signed-off-by: Fahed Dorgaa <fahed.dorgaa@gmail.com>
This commit is contained in:
Fahed Dorgaa 2019-07-13 15:07:23 +02:00
parent f2b6c31d0f
commit db95af43f3
6 changed files with 37 additions and 24 deletions

View File

@ -137,7 +137,7 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
c.conn, c.connector = conn, connector c.conn, c.connector = conn, connector
} }
if copts.services == nil && c.conn == nil { if copts.services == nil && c.conn == nil {
return nil, errors.New("no grpc connection or services is available") return nil, errdefs.ErrNoGRPCAndService
} }
// check namespace labels for default runtime // check namespace labels for default runtime
@ -196,7 +196,7 @@ type Client struct {
// Reconnect re-establishes the GRPC connection to the containerd daemon // Reconnect re-establishes the GRPC connection to the containerd daemon
func (c *Client) Reconnect() error { func (c *Client) Reconnect() error {
if c.connector == nil { if c.connector == nil {
return errors.New("unable to reconnect to containerd, no connector available") return errdefs.ErrReconnectFailed
} }
c.connMu.Lock() c.connMu.Lock()
defer c.connMu.Unlock() defer c.connMu.Unlock()
@ -219,7 +219,7 @@ func (c *Client) IsServing(ctx context.Context) (bool, error) {
c.connMu.Lock() c.connMu.Lock()
if c.conn == nil { if c.conn == nil {
c.connMu.Unlock() c.connMu.Unlock()
return false, errors.New("no grpc connection available") return false, errdefs.ErrNoGRPC
} }
c.connMu.Unlock() c.connMu.Unlock()
r, err := c.HealthService().Check(ctx, &grpc_health_v1.HealthCheckRequest{}, grpc.WaitForReady(true)) 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
} }
if fetchCtx.Unpack { if fetchCtx.Unpack {
return images.Image{}, errors.New("unpack on fetch not supported, try pull") return images.Image{}, errdefs.ErrUnpackNotSupported
} }
if fetchCtx.PlatformMatcher == nil { if fetchCtx.PlatformMatcher == nil {
@ -656,7 +656,7 @@ func (c *Client) Version(ctx context.Context) (Version, error) {
c.connMu.Lock() c.connMu.Lock()
if c.conn == nil { if c.conn == nil {
c.connMu.Unlock() c.connMu.Unlock()
return Version{}, errors.New("no grpc connection available") return Version{}, errdefs.ErrNoGRPC
} }
c.connMu.Unlock() c.connMu.Unlock()
response, err := c.VersionService().Version(ctx, &ptypes.Empty{}) response, err := c.VersionService().Version(ctx, &ptypes.Empty{})

View File

@ -27,6 +27,7 @@ import (
"runtime" "runtime"
"time" "time"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
"github.com/containerd/containerd/services/server" "github.com/containerd/containerd/services/server"
@ -152,7 +153,7 @@ func App() *cli.App {
ttrpcAddress = fmt.Sprintf("%s.ttrpc", config.GRPC.Address) ttrpcAddress = fmt.Sprintf("%s.ttrpc", config.GRPC.Address)
) )
if address == "" { if address == "" {
return errors.New("grpc address cannot be empty") return errdefs.ErrEmptyGRCPAddress
} }
log.G(ctx).WithFields(logrus.Fields{ log.G(ctx).WithFields(logrus.Fields{
"version": version.Version, "version": version.Version,

View File

@ -51,7 +51,7 @@ var publishCommand = cli.Command{
ctx := namespaces.WithNamespace(gocontext.Background(), context.String("namespace")) ctx := namespaces.WithNamespace(gocontext.Background(), context.String("namespace"))
topic := context.String("topic") topic := context.String("topic")
if topic == "" { if topic == "" {
return errors.New("topic required to publish event") return errdefs.ErrEmptyTopic
} }
payload, err := getEventPayload(os.Stdin) payload, err := getEventPayload(os.Stdin)
if err != nil { if err != nil {

View File

@ -18,7 +18,6 @@ package command
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
@ -28,6 +27,7 @@ import (
"time" "time"
"unsafe" "unsafe"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/services/server" "github.com/containerd/containerd/services/server"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/urfave/cli" "github.com/urfave/cli"
@ -162,7 +162,7 @@ func (h *etwHook) Fire(e *logrus.Entry) error {
etype = windows.EVENTLOG_INFORMATION_TYPE etype = windows.EVENTLOG_INFORMATION_TYPE
eid = eventDebug eid = eventDebug
default: default:
return errors.New("unknown level") return errdefs.ErrUnknownLevel
} }
// If there is additional data, include it as a second string. // If there is additional data, include it as a second string.
@ -311,7 +311,7 @@ func registerUnregisterService(root string) (bool, error) {
if unregisterServiceFlag { if unregisterServiceFlag {
if registerServiceFlag { if registerServiceFlag {
return true, errors.New("--register-service and --unregister-service cannot be used together") return true, errdefs.ErrRegisterAndUnregisterService
} }
return true, unregisterService() return true, unregisterService()
} }

View File

@ -28,9 +28,9 @@ import (
"github.com/containerd/containerd/cmd/ctr/commands" "github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/cmd/ctr/commands/run" "github.com/containerd/containerd/cmd/ctr/commands/run"
"github.com/containerd/containerd/containers" "github.com/containerd/containerd/containers"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
"github.com/containerd/typeurl" "github.com/containerd/typeurl"
"github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -65,17 +65,17 @@ var createCommand = cli.Command{
if config { if config {
id = context.Args().First() id = context.Args().First()
if context.NArg() > 1 { if context.NArg() > 1 {
return errors.New("with spec config file, only container id should be provided") return errdefs.ErrArgConfigFile
} }
} else { } else {
id = context.Args().Get(1) id = context.Args().Get(1)
ref = context.Args().First() ref = context.Args().First()
if ref == "" { if ref == "" {
return errors.New("image ref must be provided") return errdefs.ErrUnprovidedImageRef
} }
} }
if id == "" { if id == "" {
return errors.New("container id must be provided") return errdefs.ErrEmptyContainerId
} }
client, ctx, cancel, err := commands.NewClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
@ -168,7 +168,7 @@ var deleteCommand = cli.Command{
} }
if context.NArg() == 0 { if context.NArg() == 0 {
return errors.New("must specify at least one container to delete") return errdefs.ErrDeleteNoneContainer
} }
for _, arg := range context.Args() { for _, arg := range context.Args() {
if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil { if err := deleteContainer(ctx, client, arg, deleteOpts...); err != nil {
@ -214,7 +214,7 @@ var setLabelsCommand = cli.Command{
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
containerID, labels := commands.ObjectWithLabelArgs(context) containerID, labels := commands.ObjectWithLabelArgs(context)
if containerID == "" { if containerID == "" {
return errors.New("container id must be provided") return errdefs.ErrEmptyContainerId
} }
client, ctx, cancel, err := commands.NewClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {
@ -250,7 +250,7 @@ var infoCommand = cli.Command{
Action: func(context *cli.Context) error { Action: func(context *cli.Context) error {
id := context.Args().First() id := context.Args().First()
if id == "" { if id == "" {
return errors.New("container id must be provided") return errdefs.ErrEmptyContainerId
} }
client, ctx, cancel, err := commands.NewClient(context) client, ctx, cancel, err := commands.NewClient(context)
if err != nil { if err != nil {

View File

@ -40,13 +40,25 @@ import (
// For the most part, we just try to provide local grpc errors. Most conditions // For the most part, we just try to provide local grpc errors. Most conditions
// map very well to those defined by grpc. // map very well to those defined by grpc.
var ( var (
ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping. ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping.
ErrInvalidArgument = errors.New("invalid argument") ErrInvalidArgument = errors.New("invalid argument")
ErrNotFound = errors.New("not found") ErrNotFound = errors.New("not found")
ErrAlreadyExists = errors.New("already exists") ErrAlreadyExists = errors.New("already exists")
ErrFailedPrecondition = errors.New("failed precondition") ErrFailedPrecondition = errors.New("failed precondition")
ErrUnavailable = errors.New("unavailable") ErrUnavailable = errors.New("unavailable")
ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented
ErrNoGRPCAndService = errors.New("no grpc connection and services is available")
ErrNoGRPC = errors.New("no grpc connection available")
ErrReconnectFailed = errors.New("unable to reconnect to containerd, no connector available")
ErrUnpackNotSupported = errors.New("unpack on fetch not supported, try pull")
ErrEmptyGRCPAddress = errors.New("grpc address cannot be empty")
ErrEmptyTopic = errors.New("topic required to publish event")
ErrUnknownLevel = errors.New("unknown level")
ErrRegisterAndUnregisterService = errors.New("--register-service and --unregister-service cannot be used together")
ErrArgConfigFile = errors.New("with spec config file, only container id should be provided")
ErrUnprovidedImageRef = errors.New("image ref must be provided")
ErrEmptyContainerId = errors.New("container id must be provided")
ErrDeleteNoneContainer = errors.New("must specify at least one container to delete")
) )
// IsInvalidArgument returns true if the error is due to an invalid argument // IsInvalidArgument returns true if the error is due to an invalid argument