error.go 4.7 KB

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