Add custom error struct for Group/Version not found

Kubernetes-commit: dc83af0b44bd6432dea3ccd29a77ca3a0b6122a2
This commit is contained in:
Sean Sullivan 2023-03-03 00:01:22 +00:00 committed by Kubernetes Publisher
parent 3f4372de09
commit 4c5eaaf0d7
2 changed files with 19 additions and 9 deletions

View File

@ -125,7 +125,7 @@ func (r *root) retrieveGVBytes(gv schema.GroupVersion) ([]byte, error) {
apiPath := gvToAPIPath(gv)
gvOpenAPI, found := paths[apiPath]
if !found {
return nil, fmt.Errorf("GroupVersion (%s) not found in OpenAPI V3 root document", gv)
return nil, &GroupVersionNotFoundError{gv: gv}
}
return gvOpenAPI.Schema(runtime.ContentTypeJSON)
}
@ -170,3 +170,13 @@ func pathToGroupVersion(path string) (schema.GroupVersion, error) {
}
return gv, nil
}
// Encapsulates GroupVersion not found as one of the paths
// at OpenAPI V3 endpoint.
type GroupVersionNotFoundError struct {
gv schema.GroupVersion
}
func (r *GroupVersionNotFoundError) Error() string {
return fmt.Sprintf("GroupVersion (%v) not found as OpenAPI V3 path", r.gv)
}

View File

@ -113,7 +113,7 @@ func TestOpenAPIV3Root_GVSpec(t *testing.T) {
name string
gv schema.GroupVersion
expectedPaths []string
err bool
err error
}{
{
name: "OpenAPI V3 for apps/v1 works",
@ -144,7 +144,7 @@ func TestOpenAPIV3Root_GVSpec(t *testing.T) {
{
name: "OpenAPI V3 spec not found",
gv: schema.GroupVersion{Group: "not", Version: "found"},
err: true,
err: &GroupVersionNotFoundError{gv: schema.GroupVersion{Group: "not", Version: "found"}},
},
}
@ -153,8 +153,8 @@ func TestOpenAPIV3Root_GVSpec(t *testing.T) {
client := openapitest.NewFileClient(t)
root := NewRoot(client)
gvSpec, err := root.GVSpec(test.gv)
if test.err {
require.Error(t, err)
if test.err != nil {
assert.True(t, reflect.DeepEqual(test.err, err))
return
}
require.NoError(t, err)
@ -172,7 +172,7 @@ func TestOpenAPIV3Root_GVSpecAsMap(t *testing.T) {
name string
gv schema.GroupVersion
expectedPaths []string
err bool
err error
}{
{
name: "OpenAPI V3 for apps/v1 works",
@ -203,7 +203,7 @@ func TestOpenAPIV3Root_GVSpecAsMap(t *testing.T) {
{
name: "OpenAPI V3 spec not found",
gv: schema.GroupVersion{Group: "not", Version: "found"},
err: true,
err: &GroupVersionNotFoundError{gv: schema.GroupVersion{Group: "not", Version: "found"}},
},
}
@ -212,8 +212,8 @@ func TestOpenAPIV3Root_GVSpecAsMap(t *testing.T) {
client := openapitest.NewFileClient(t)
root := NewRoot(client)
gvSpecAsMap, err := root.GVSpecAsMap(test.gv)
if test.err {
require.Error(t, err)
if test.err != nil {
assert.True(t, reflect.DeepEqual(test.err, err))
return
}
require.NoError(t, err)