errors.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 meta
  14. import (
  15. "fmt"
  16. "k8s.io/kubernetes/pkg/api/unversioned"
  17. )
  18. // AmbiguousResourceError is returned if the RESTMapper finds multiple matches for a resource
  19. type AmbiguousResourceError struct {
  20. PartialResource unversioned.GroupVersionResource
  21. MatchingResources []unversioned.GroupVersionResource
  22. MatchingKinds []unversioned.GroupVersionKind
  23. }
  24. func (e *AmbiguousResourceError) Error() string {
  25. switch {
  26. case len(e.MatchingKinds) > 0 && len(e.MatchingResources) > 0:
  27. return fmt.Sprintf("%v matches multiple resources %v and kinds %v", e.PartialResource, e.MatchingResources, e.MatchingKinds)
  28. case len(e.MatchingKinds) > 0:
  29. return fmt.Sprintf("%v matches multiple kinds %v", e.PartialResource, e.MatchingKinds)
  30. case len(e.MatchingResources) > 0:
  31. return fmt.Sprintf("%v matches multiple resources %v", e.PartialResource, e.MatchingResources)
  32. }
  33. return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialResource)
  34. }
  35. // AmbiguousKindError is returned if the RESTMapper finds multiple matches for a kind
  36. type AmbiguousKindError struct {
  37. PartialKind unversioned.GroupVersionKind
  38. MatchingResources []unversioned.GroupVersionResource
  39. MatchingKinds []unversioned.GroupVersionKind
  40. }
  41. func (e *AmbiguousKindError) Error() string {
  42. switch {
  43. case len(e.MatchingKinds) > 0 && len(e.MatchingResources) > 0:
  44. return fmt.Sprintf("%v matches multiple resources %v and kinds %v", e.PartialKind, e.MatchingResources, e.MatchingKinds)
  45. case len(e.MatchingKinds) > 0:
  46. return fmt.Sprintf("%v matches multiple kinds %v", e.PartialKind, e.MatchingKinds)
  47. case len(e.MatchingResources) > 0:
  48. return fmt.Sprintf("%v matches multiple resources %v", e.PartialKind, e.MatchingResources)
  49. }
  50. return fmt.Sprintf("%v matches multiple resources or kinds", e.PartialKind)
  51. }
  52. func IsAmbiguousError(err error) bool {
  53. if err == nil {
  54. return false
  55. }
  56. switch err.(type) {
  57. case *AmbiguousResourceError, *AmbiguousKindError:
  58. return true
  59. default:
  60. return false
  61. }
  62. }
  63. // NoResourceMatchError is returned if the RESTMapper can't find any match for a resource
  64. type NoResourceMatchError struct {
  65. PartialResource unversioned.GroupVersionResource
  66. }
  67. func (e *NoResourceMatchError) Error() string {
  68. return fmt.Sprintf("no matches for %v", e.PartialResource)
  69. }
  70. // NoKindMatchError is returned if the RESTMapper can't find any match for a kind
  71. type NoKindMatchError struct {
  72. PartialKind unversioned.GroupVersionKind
  73. }
  74. func (e *NoKindMatchError) Error() string {
  75. return fmt.Sprintf("no matches for %v", e.PartialKind)
  76. }
  77. func IsNoMatchError(err error) bool {
  78. if err == nil {
  79. return false
  80. }
  81. switch err.(type) {
  82. case *NoResourceMatchError, *NoKindMatchError:
  83. return true
  84. default:
  85. return false
  86. }
  87. }