Add a strongly typed error for unrecognized kind/type/version

This commit is contained in:
Clayton Coleman
2014-12-03 08:40:30 -05:00
parent 3910b2d6e1
commit 84d84c50c2
4 changed files with 84 additions and 6 deletions

View File

@@ -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
View 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
}

View File

@@ -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, &notRegisteredErr{kind: kind, version: versionName}
}
return nil, fmt.Errorf("no version '%v'", versionName)
return nil, &notRegisteredErr{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 "", "", &notRegisteredErr{t: t}
}
apiVersion = version
kind = kinds[0]

27
pkg/runtime/error.go Normal file
View 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)
}