diff --git a/pkg/conversion/decode.go b/pkg/conversion/decode.go index 81be6bb5bef..40987becdb5 100644 --- a/pkg/conversion/decode.go +++ b/pkg/conversion/decode.go @@ -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. diff --git a/pkg/conversion/error.go b/pkg/conversion/error.go new file mode 100644 index 00000000000..7ee24ee4380 --- /dev/null +++ b/pkg/conversion/error.go @@ -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 +} diff --git a/pkg/conversion/scheme.go b/pkg/conversion/scheme.go index a42c83ed32e..5bffb03b377 100644 --- a/pkg/conversion/scheme.go +++ b/pkg/conversion/scheme.go @@ -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] diff --git a/pkg/runtime/error.go b/pkg/runtime/error.go new file mode 100644 index 00000000000..a84fe6ae9f3 --- /dev/null +++ b/pkg/runtime/error.go @@ -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) +}