mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-09 05:01:46 +00:00
Add a strongly typed error for unrecognized kind/type/version
This commit is contained in:
@@ -115,7 +115,7 @@ func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
|
||||
} else {
|
||||
external, err := s.NewObject(dataVersion, dataKind)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create new object of type ('%s', '%s')", dataVersion, dataKind)
|
||||
return err
|
||||
}
|
||||
// yaml is a superset of json, so we use it to decode here. That way,
|
||||
// we understand both.
|
||||
|
51
pkg/conversion/error.go
Normal file
51
pkg/conversion/error.go
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package conversion
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
type notRegisteredErr struct {
|
||||
kind string
|
||||
version string
|
||||
t reflect.Type
|
||||
}
|
||||
|
||||
func (k *notRegisteredErr) Error() string {
|
||||
if k.t != nil {
|
||||
return fmt.Sprintf("no kind is registered for the type %v", k.t)
|
||||
}
|
||||
if len(k.kind) == 0 {
|
||||
return fmt.Sprintf("no version %q has been registered", k.version)
|
||||
}
|
||||
if len(k.version) == 0 {
|
||||
return fmt.Sprintf("no kind %q is registered for the default version", k.kind)
|
||||
}
|
||||
return fmt.Sprintf("no kind %q is registered for version %q", k.kind, k.version)
|
||||
}
|
||||
|
||||
// IsNotRegisteredError returns true if the error indicates the provided
|
||||
// object or input data is not registered.
|
||||
func IsNotRegisteredError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := err.(*notRegisteredErr)
|
||||
return ok
|
||||
}
|
@@ -143,14 +143,14 @@ func (s *Scheme) KnownTypes(version string) map[string]reflect.Type {
|
||||
|
||||
// NewObject returns a new object of the given version and name,
|
||||
// or an error if it hasn't been registered.
|
||||
func (s *Scheme) NewObject(versionName, typeName string) (interface{}, error) {
|
||||
func (s *Scheme) NewObject(versionName, kind string) (interface{}, error) {
|
||||
if types, ok := s.versionMap[versionName]; ok {
|
||||
if t, ok := types[typeName]; ok {
|
||||
if t, ok := types[kind]; ok {
|
||||
return reflect.New(t).Interface(), nil
|
||||
}
|
||||
return nil, fmt.Errorf("no type '%v' for version '%v'", typeName, versionName)
|
||||
return nil, ¬RegisteredErr{kind: kind, version: versionName}
|
||||
}
|
||||
return nil, fmt.Errorf("no version '%v'", versionName)
|
||||
return nil, ¬RegisteredErr{kind: kind, version: versionName}
|
||||
}
|
||||
|
||||
// AddConversionFuncs adds functions to the list of conversion functions. The given
|
||||
@@ -284,7 +284,7 @@ func (s *Scheme) ObjectVersionAndKind(obj interface{}) (apiVersion, kind string,
|
||||
version, vOK := s.typeToVersion[t]
|
||||
kinds, kOK := s.typeToKind[t]
|
||||
if !vOK || !kOK {
|
||||
return "", "", fmt.Errorf("unregistered type: %v", t)
|
||||
return "", "", ¬RegisteredErr{t: t}
|
||||
}
|
||||
apiVersion = version
|
||||
kind = kinds[0]
|
||||
|
27
pkg/runtime/error.go
Normal file
27
pkg/runtime/error.go
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
Copyright 2014 Google Inc. All rights reserved.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package runtime
|
||||
|
||||
import (
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"
|
||||
)
|
||||
|
||||
// IsNotRegisteredError returns true if the error indicates the provided
|
||||
// object or input data is not registered.
|
||||
func IsNotRegisteredError(err error) bool {
|
||||
return conversion.IsNotRegisteredError(err)
|
||||
}
|
Reference in New Issue
Block a user