From ac749aa0bd2b74c9dd5d17d9c6cca7629b394220 Mon Sep 17 00:00:00 2001 From: David Simansky Date: Fri, 29 Oct 2021 22:45:25 +0200 Subject: [PATCH] Reword missing API error to mention client update --- pkg/errors/errors.go | 15 +++++++++++++-- pkg/errors/errors_test.go | 15 ++++++++------- pkg/errors/factory.go | 2 +- pkg/errors/factory_test.go | 2 +- pkg/kn/commands/channel/list_types.go | 3 +-- pkg/kn/commands/channel/list_types_test.go | 4 ++-- pkg/kn/commands/source/list_test.go | 2 +- pkg/kn/commands/source/list_types.go | 3 +-- 8 files changed, 28 insertions(+), 18 deletions(-) diff --git a/pkg/errors/errors.go b/pkg/errors/errors.go index e58f3b4cc7..69543f2cb0 100644 --- a/pkg/errors/errors.go +++ b/pkg/errors/errors.go @@ -17,12 +17,15 @@ package errors import ( "fmt" "strings" + "unicode" + "unicode/utf8" ) -func newInvalidCRD(apiGroup string) *KNError { +func NewInvalidCRD(apiGroup string) *KNError { parts := strings.Split(apiGroup, ".") name := parts[0] - msg := fmt.Sprintf("no Knative %s API found on the backend, please verify the installation", name) + msg := fmt.Sprintf("no Knative %s API found on the backend, please verify the installation or "+ + "update the 'kn' client to latest version", firstCharToUpper(name)) return NewKNError(msg) } @@ -37,3 +40,11 @@ func newNoRouteToHost(errString string) *KNError { func newNoKubeConfig(errString string) *KNError { return NewKNError("no kubeconfig has been provided, please use a valid configuration to connect to the cluster") } + +func firstCharToUpper(s string) string { + if len(s) == 0 { + return s + } + r, n := utf8.DecodeRuneInString(s) + return string(unicode.ToUpper(r)) + s[n:] +} diff --git a/pkg/errors/errors_test.go b/pkg/errors/errors_test.go index 6ed5007322..7e27a5c908 100644 --- a/pkg/errors/errors_test.go +++ b/pkg/errors/errors_test.go @@ -17,17 +17,18 @@ package errors import ( "testing" + "knative.dev/client/pkg/util" + "gotest.tools/v3/assert" ) func TestNewInvalidCRD(t *testing.T) { - err := newInvalidCRD("serving.knative.dev") - assert.Error(t, err, "no Knative serving API found on the backend, please verify the installation") - - err = newInvalidCRD("eventing") - assert.Error(t, err, "no Knative eventing API found on the backend, please verify the installation") + err := NewInvalidCRD("serving.knative.dev") + assert.Assert(t, util.ContainsAll(err.Error(), "no Knative Serving API found on the backend", "please verify the installation", "update", "'kn'")) - err = newInvalidCRD("") - assert.Error(t, err, "no Knative API found on the backend, please verify the installation") + err = NewInvalidCRD("eventing") + assert.Assert(t, util.ContainsAll(err.Error(), "no Knative Eventing API found on the backend", "please verify the installation", "update", "'kn'")) + err = NewInvalidCRD("") + assert.Assert(t, util.ContainsAll(err.Error(), "no Knative API found on the backend", "please verify the installation", "update", "'kn'")) } diff --git a/pkg/errors/factory.go b/pkg/errors/factory.go index 0c8428f817..279e1912f4 100644 --- a/pkg/errors/factory.go +++ b/pkg/errors/factory.go @@ -54,7 +54,7 @@ func newStatusError(err error) error { } var knerr *KNError if isCRDError(errAPIStatus) { - knerr = newInvalidCRD(errAPIStatus.Status().Details.Group) + knerr = NewInvalidCRD(errAPIStatus.Status().Details.Group) knerr.Status = errAPIStatus return knerr } diff --git a/pkg/errors/factory_test.go b/pkg/errors/factory_test.go index 93a5e92531..a3e0a20062 100644 --- a/pkg/errors/factory_test.go +++ b/pkg/errors/factory_test.go @@ -61,7 +61,7 @@ func TestKnErrorsStatusErrors(t *testing.T) { } return statusError }, - ExpectedMsg: "no Knative serving API found on the backend, please verify the installation", + ExpectedMsg: "no Knative Serving API found on the backend, please verify the installation or update the 'kn' client to latest version", Validate: func(t *testing.T, err error, msg string) { assert.Error(t, err, msg) }, diff --git a/pkg/kn/commands/channel/list_types.go b/pkg/kn/commands/channel/list_types.go index aa75f9ff59..944504224c 100644 --- a/pkg/kn/commands/channel/list_types.go +++ b/pkg/kn/commands/channel/list_types.go @@ -17,7 +17,6 @@ package channel import ( "context" - "fmt" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -66,7 +65,7 @@ func NewChannelListTypesCommand(p *commands.KnParams) *cobra.Command { channelListTypes = &unstructured.UnstructuredList{} } if !listTypesFlags.GenericPrintFlags.OutputFlagSpecified() && len(channelListTypes.Items) == 0 { - return fmt.Errorf("no channels found on the backend, please verify the installation") + return knerrors.NewInvalidCRD("Channels") } if channelListTypes.GroupVersionKind().Empty() { diff --git a/pkg/kn/commands/channel/list_types_test.go b/pkg/kn/commands/channel/list_types_test.go index d3d73f3164..49d4e055d4 100644 --- a/pkg/kn/commands/channel/list_types_test.go +++ b/pkg/kn/commands/channel/list_types_test.go @@ -68,7 +68,7 @@ func TestChannelListTypesNoChannelInstalled(t *testing.T) { _, err := channelFakeCmd([]string{"channel", "list-types"}, dynamicClient) assert.Check(t, err != nil) - assert.Check(t, util.ContainsAll(err.Error(), "no channels found on the backend, please verify the installation")) + assert.Check(t, util.ContainsAll(err.Error(), "no", "Knative Channels", "found", "backend", "verify", "installation")) } func TestChannelListTypesNoChannelWithJsonOutput(t *testing.T) { @@ -86,7 +86,7 @@ func TestChannelListTypesErrorDynamicClient(t *testing.T) { _, err := channelFakeCmd([]string{"channel", "list-types"}, dynamicClient) assert.Check(t, err != nil) - assert.Check(t, util.ContainsAll(err.Error(), "no channels found on the backend, please verify the installation")) + assert.Check(t, util.ContainsAll(err.Error(), "no", "Knative Channels", "found", "backend", "verify", "installation")) } func TestChannelListTypes(t *testing.T) { diff --git a/pkg/kn/commands/source/list_test.go b/pkg/kn/commands/source/list_test.go index 3874444125..9a568f6afb 100644 --- a/pkg/kn/commands/source/list_test.go +++ b/pkg/kn/commands/source/list_test.go @@ -56,7 +56,7 @@ func sourceFakeCmd(args []string, objects ...runtime.Object) (output []string, e func TestSourceListTypesNoSourcesInstalled(t *testing.T) { _, err := sourceFakeCmd([]string{"source", "list-types"}) assert.Check(t, err != nil) - assert.Check(t, util.ContainsAll(err.Error(), "no sources", "found", "backend", "verify", "installation")) + assert.Check(t, util.ContainsAll(err.Error(), "no", "Knative Sources", "found", "backend", "verify", "installation")) } func TestSourceListTypesNoSourcesWithJsonOutput(t *testing.T) { diff --git a/pkg/kn/commands/source/list_types.go b/pkg/kn/commands/source/list_types.go index 172fef1230..f0fbb1108b 100644 --- a/pkg/kn/commands/source/list_types.go +++ b/pkg/kn/commands/source/list_types.go @@ -16,7 +16,6 @@ package source import ( "context" - "fmt" "knative.dev/client/pkg/sources" @@ -68,7 +67,7 @@ func NewListTypesCommand(p *commands.KnParams) *cobra.Command { } if !listTypesFlags.GenericPrintFlags.OutputFlagSpecified() && len(sourceListTypes.Items) == 0 { - return fmt.Errorf("no sources found on the backend, please verify the installation") + return knerrors.NewInvalidCRD("Sources") } if sourceListTypes.GroupVersionKind().Empty() {