errors.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2017 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 mergepatch
  14. import (
  15. "errors"
  16. "fmt"
  17. "reflect"
  18. )
  19. var (
  20. ErrBadJSONDoc = errors.New("invalid JSON document")
  21. ErrNoListOfLists = errors.New("lists of lists are not supported")
  22. ErrBadPatchFormatForPrimitiveList = errors.New("invalid patch format of primitive list")
  23. ErrBadPatchFormatForRetainKeys = errors.New("invalid patch format of retainKeys")
  24. ErrBadPatchFormatForSetElementOrderList = errors.New("invalid patch format of setElementOrder list")
  25. ErrPatchContentNotMatchRetainKeys = errors.New("patch content doesn't match retainKeys list")
  26. ErrUnsupportedStrategicMergePatchFormat = errors.New("strategic merge patch format is not supported")
  27. )
  28. func ErrNoMergeKey(m map[string]interface{}, k string) error {
  29. return fmt.Errorf("map: %v does not contain declared merge key: %s", m, k)
  30. }
  31. func ErrBadArgType(expected, actual interface{}) error {
  32. return fmt.Errorf("expected a %s, but received a %s",
  33. reflect.TypeOf(expected),
  34. reflect.TypeOf(actual))
  35. }
  36. func ErrBadArgKind(expected, actual interface{}) error {
  37. var expectedKindString, actualKindString string
  38. if expected == nil {
  39. expectedKindString = "nil"
  40. } else {
  41. expectedKindString = reflect.TypeOf(expected).Kind().String()
  42. }
  43. if actual == nil {
  44. actualKindString = "nil"
  45. } else {
  46. actualKindString = reflect.TypeOf(actual).Kind().String()
  47. }
  48. return fmt.Errorf("expected a %s, but received a %s", expectedKindString, actualKindString)
  49. }
  50. func ErrBadPatchType(t interface{}, m map[string]interface{}) error {
  51. return fmt.Errorf("unknown patch type: %s in map: %v", t, m)
  52. }
  53. // IsPreconditionFailed returns true if the provided error indicates
  54. // a precondition failed.
  55. func IsPreconditionFailed(err error) bool {
  56. _, ok := err.(ErrPreconditionFailed)
  57. return ok
  58. }
  59. type ErrPreconditionFailed struct {
  60. message string
  61. }
  62. func NewErrPreconditionFailed(target map[string]interface{}) ErrPreconditionFailed {
  63. s := fmt.Sprintf("precondition failed for: %v", target)
  64. return ErrPreconditionFailed{s}
  65. }
  66. func (err ErrPreconditionFailed) Error() string {
  67. return err.message
  68. }
  69. type ErrConflict struct {
  70. message string
  71. }
  72. func NewErrConflict(patch, current string) ErrConflict {
  73. s := fmt.Sprintf("patch:\n%s\nconflicts with changes made from original to current:\n%s\n", patch, current)
  74. return ErrConflict{s}
  75. }
  76. func (err ErrConflict) Error() string {
  77. return err.message
  78. }
  79. // IsConflict returns true if the provided error indicates
  80. // a conflict between the patch and the current configuration.
  81. func IsConflict(err error) bool {
  82. _, ok := err.(ErrConflict)
  83. return ok
  84. }