Commit 84d84c50 authored by Clayton Coleman's avatar Clayton Coleman

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

parent 3910b2d6
...@@ -115,7 +115,7 @@ func (s *Scheme) DecodeInto(data []byte, obj interface{}) error { ...@@ -115,7 +115,7 @@ func (s *Scheme) DecodeInto(data []byte, obj interface{}) error {
} else { } else {
external, err := s.NewObject(dataVersion, dataKind) external, err := s.NewObject(dataVersion, dataKind)
if err != nil { 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, // yaml is a superset of json, so we use it to decode here. That way,
// we understand both. // we understand both.
......
/*
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 { ...@@ -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, // NewObject returns a new object of the given version and name,
// or an error if it hasn't been registered. // 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 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 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 // AddConversionFuncs adds functions to the list of conversion functions. The given
...@@ -284,7 +284,7 @@ func (s *Scheme) ObjectVersionAndKind(obj interface{}) (apiVersion, kind string, ...@@ -284,7 +284,7 @@ func (s *Scheme) ObjectVersionAndKind(obj interface{}) (apiVersion, kind string,
version, vOK := s.typeToVersion[t] version, vOK := s.typeToVersion[t]
kinds, kOK := s.typeToKind[t] kinds, kOK := s.typeToKind[t]
if !vOK || !kOK { if !vOK || !kOK {
return "", "", fmt.Errorf("unregistered type: %v", t) return "", "", &notRegisteredErr{t: t}
} }
apiVersion = version apiVersion = version
kind = kinds[0] kind = kinds[0]
......
/*
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)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment