error.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/apimachinery/pkg/runtime/schema"
  18. )
  19. type notRegisteredErr struct {
  20. schemeName string
  21. gvk schema.GroupVersionKind
  22. target GroupVersioner
  23. t reflect.Type
  24. }
  25. func NewNotRegisteredErrForKind(schemeName string, gvk schema.GroupVersionKind) error {
  26. return &notRegisteredErr{schemeName: schemeName, gvk: gvk}
  27. }
  28. func NewNotRegisteredErrForType(schemeName string, t reflect.Type) error {
  29. return &notRegisteredErr{schemeName: schemeName, t: t}
  30. }
  31. func NewNotRegisteredErrForTarget(schemeName string, t reflect.Type, target GroupVersioner) error {
  32. return &notRegisteredErr{schemeName: schemeName, t: t, target: target}
  33. }
  34. func NewNotRegisteredGVKErrForTarget(schemeName string, gvk schema.GroupVersionKind, target GroupVersioner) error {
  35. return &notRegisteredErr{schemeName: schemeName, gvk: gvk, target: target}
  36. }
  37. func (k *notRegisteredErr) Error() string {
  38. if k.t != nil && k.target != nil {
  39. return fmt.Sprintf("%v is not suitable for converting to %q in scheme %q", k.t, k.target, k.schemeName)
  40. }
  41. nullGVK := schema.GroupVersionKind{}
  42. if k.gvk != nullGVK && k.target != nil {
  43. return fmt.Sprintf("%q is not suitable for converting to %q in scheme %q", k.gvk.GroupVersion(), k.target, k.schemeName)
  44. }
  45. if k.t != nil {
  46. return fmt.Sprintf("no kind is registered for the type %v in scheme %q", k.t, k.schemeName)
  47. }
  48. if len(k.gvk.Kind) == 0 {
  49. return fmt.Sprintf("no version %q has been registered in scheme %q", k.gvk.GroupVersion(), k.schemeName)
  50. }
  51. if k.gvk.Version == APIVersionInternal {
  52. return fmt.Sprintf("no kind %q is registered for the internal version of group %q in scheme %q", k.gvk.Kind, k.gvk.Group, k.schemeName)
  53. }
  54. return fmt.Sprintf("no kind %q is registered for version %q in scheme %q", k.gvk.Kind, k.gvk.GroupVersion(), k.schemeName)
  55. }
  56. // IsNotRegisteredError returns true if the error indicates the provided
  57. // object or input data is not registered.
  58. func IsNotRegisteredError(err error) bool {
  59. if err == nil {
  60. return false
  61. }
  62. _, ok := err.(*notRegisteredErr)
  63. return ok
  64. }
  65. type missingKindErr struct {
  66. data string
  67. }
  68. func NewMissingKindErr(data string) error {
  69. return &missingKindErr{data}
  70. }
  71. func (k *missingKindErr) Error() string {
  72. return fmt.Sprintf("Object 'Kind' is missing in '%s'", k.data)
  73. }
  74. // IsMissingKind returns true if the error indicates that the provided object
  75. // is missing a 'Kind' field.
  76. func IsMissingKind(err error) bool {
  77. if err == nil {
  78. return false
  79. }
  80. _, ok := err.(*missingKindErr)
  81. return ok
  82. }
  83. type missingVersionErr struct {
  84. data string
  85. }
  86. func NewMissingVersionErr(data string) error {
  87. return &missingVersionErr{data}
  88. }
  89. func (k *missingVersionErr) Error() string {
  90. return fmt.Sprintf("Object 'apiVersion' is missing in '%s'", k.data)
  91. }
  92. // IsMissingVersion returns true if the error indicates that the provided object
  93. // is missing a 'Version' field.
  94. func IsMissingVersion(err error) bool {
  95. if err == nil {
  96. return false
  97. }
  98. _, ok := err.(*missingVersionErr)
  99. return ok
  100. }
  101. // strictDecodingError is a base error type that is returned by a strict Decoder such
  102. // as UniversalStrictDecoder.
  103. type strictDecodingError struct {
  104. message string
  105. data string
  106. }
  107. // NewStrictDecodingError creates a new strictDecodingError object.
  108. func NewStrictDecodingError(message string, data string) error {
  109. return &strictDecodingError{
  110. message: message,
  111. data: data,
  112. }
  113. }
  114. func (e *strictDecodingError) Error() string {
  115. return fmt.Sprintf("strict decoder error for %s: %s", e.data, e.message)
  116. }
  117. // IsStrictDecodingError returns true if the error indicates that the provided object
  118. // strictness violations.
  119. func IsStrictDecodingError(err error) bool {
  120. if err == nil {
  121. return false
  122. }
  123. _, ok := err.(*strictDecodingError)
  124. return ok
  125. }