Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom error struct for Group/Version not found #116237

Merged
merged 1 commit into from Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion staging/src/k8s.io/client-go/openapi3/root.go
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be public?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If others outside the package want to check the type, they would have to cast to GroupVersionNotFoundError; so I think it has to be public. Unless we create and export a IsGroupVersionNotFoundError function in this package.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't referring to the type, but to the gv variable

}

func (r *GroupVersionNotFoundError) Error() string {
return fmt.Sprintf("GroupVersion (%v) not found as OpenAPI V3 path", r.gv)
}
16 changes: 8 additions & 8 deletions staging/src/k8s.io/client-go/openapi3/root_test.go
Expand Up @@ -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",
Expand Down Expand Up @@ -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"}},
},
}

Expand All @@ -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)
Expand All @@ -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",
Expand Down Expand Up @@ -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"}},
},
}

Expand All @@ -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)
Expand Down