feat(cli-runtime): wrap meta.NoKindMatchErrors

This commit is contained in:
Ivo Gosemann 2024-01-03 11:53:18 +01:00
parent c015565db3
commit 10a6885da1
No known key found for this signature in database
3 changed files with 4 additions and 3 deletions

View File

@ -803,7 +803,7 @@ func (b *Builder) mappingFor(resourceOrKindArg string) (*meta.RESTMapping, error
// if the error is _not_ a *meta.NoKindMatchError, then we had trouble doing discovery,
// so we should return the original error since it may help a user diagnose what is actually wrong
if meta.IsNoMatchError(err) {
return nil, fmt.Errorf("the server doesn't have a resource type %q", groupResource.Resource)
return nil, fmt.Errorf("the server doesn't have a resource type %q: %w", groupResource.Resource, err)
}
return nil, err
}

View File

@ -46,6 +46,7 @@ import (
"k8s.io/client-go/rest/fake"
restclientwatch "k8s.io/client-go/rest/watch"
"k8s.io/client-go/restmapper"
// TODO we need to remove this linkage and create our own scheme
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes/scheme"
@ -1050,7 +1051,7 @@ func TestRestMappingErrors(t *testing.T) {
// ensure that requesting a resource we _know_ not to exist results in an expected *meta.NoKindMatchError
err := b.Do().IntoSingleItemImplied(&singleItemImplied).Visit(test.Handle)
if err != nil {
if !strings.Contains(err.Error(), "server doesn't have a resource type \"foo\"") {
if !errors.Is(err, &meta.NoKindMatchError{}) {
t.Fatalf("unexpected error: %v", err)
}
}

View File

@ -66,7 +66,7 @@ func (m *mapper) infoForData(data []byte, source string) (*Info, error) {
mapping, err := restMapper.RESTMapping(gvk.GroupKind(), gvk.Version)
if err != nil {
if _, ok := err.(*meta.NoKindMatchError); ok {
return nil, fmt.Errorf("resource mapping not found for name: %q namespace: %q from %q: %v\nensure CRDs are installed first",
return nil, fmt.Errorf("resource mapping not found for name: %q namespace: %q from %q: %w\nensure CRDs are installed first",
name, namespace, source, err)
}
return nil, fmt.Errorf("unable to recognize %q: %v", source, err)