error.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package runtime
  14. import (
  15. "fmt"
  16. "reflect"
  17. "k8s.io/kubernetes/pkg/api/unversioned"
  18. )
  19. type notRegisteredErr struct {
  20. gvk unversioned.GroupVersionKind
  21. t reflect.Type
  22. }
  23. // NewNotRegisteredErr is exposed for testing.
  24. func NewNotRegisteredErr(gvk unversioned.GroupVersionKind, t reflect.Type) error {
  25. return &notRegisteredErr{gvk: gvk, t: t}
  26. }
  27. func (k *notRegisteredErr) Error() string {
  28. if k.t != nil {
  29. return fmt.Sprintf("no kind is registered for the type %v", k.t)
  30. }
  31. if len(k.gvk.Kind) == 0 {
  32. return fmt.Sprintf("no version %q has been registered", k.gvk.GroupVersion())
  33. }
  34. if k.gvk.Version == APIVersionInternal {
  35. return fmt.Sprintf("no kind %q is registered for the internal version of group %q", k.gvk.Kind, k.gvk.Group)
  36. }
  37. return fmt.Sprintf("no kind %q is registered for version %q", k.gvk.Kind, k.gvk.GroupVersion())
  38. }
  39. // IsNotRegisteredError returns true if the error indicates the provided
  40. // object or input data is not registered.
  41. func IsNotRegisteredError(err error) bool {
  42. if err == nil {
  43. return false
  44. }
  45. _, ok := err.(*notRegisteredErr)
  46. return ok
  47. }
  48. type missingKindErr struct {
  49. data string
  50. }
  51. func NewMissingKindErr(data string) error {
  52. return &missingKindErr{data}
  53. }
  54. func (k *missingKindErr) Error() string {
  55. return fmt.Sprintf("Object 'Kind' is missing in '%s'", k.data)
  56. }
  57. // IsMissingKind returns true if the error indicates that the provided object
  58. // is missing a 'Kind' field.
  59. func IsMissingKind(err error) bool {
  60. if err == nil {
  61. return false
  62. }
  63. _, ok := err.(*missingKindErr)
  64. return ok
  65. }
  66. type missingVersionErr struct {
  67. data string
  68. }
  69. // IsMissingVersion returns true if the error indicates that the provided object
  70. // is missing a 'Version' field.
  71. func NewMissingVersionErr(data string) error {
  72. return &missingVersionErr{data}
  73. }
  74. func (k *missingVersionErr) Error() string {
  75. return fmt.Sprintf("Object 'apiVersion' is missing in '%s'", k.data)
  76. }
  77. func IsMissingVersion(err error) bool {
  78. if err == nil {
  79. return false
  80. }
  81. _, ok := err.(*missingVersionErr)
  82. return ok
  83. }