Skip to content

Commit

Permalink
Reword missing API error to mention client update
Browse files Browse the repository at this point in the history
  • Loading branch information
dsimansk committed Oct 29, 2021
1 parent 6c2f202 commit ac749aa
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 18 deletions.
15 changes: 13 additions & 2 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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:]
}
15 changes: 8 additions & 7 deletions pkg/errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'"))
}
2 changes: 1 addition & 1 deletion pkg/errors/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/errors/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
3 changes: 1 addition & 2 deletions pkg/kn/commands/channel/list_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package channel

import (
"context"
"fmt"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/kn/commands/channel/list_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/kn/commands/source/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/kn/commands/source/list_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package source

import (
"context"
"fmt"

"knative.dev/client/pkg/sources"

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit ac749aa

Please sign in to comment.