github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/util/mergepatch/errors.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package mergepatch 18 19 import ( 20 "errors" 21 "fmt" 22 "reflect" 23 ) 24 25 var ( 26 ErrBadJSONDoc = errors.New("invalid JSON document") 27 ErrNoListOfLists = errors.New("lists of lists are not supported") 28 ErrBadPatchFormatForPrimitiveList = errors.New("invalid patch format of primitive list") 29 ErrBadPatchFormatForRetainKeys = errors.New("invalid patch format of retainKeys") 30 ErrBadPatchFormatForSetElementOrderList = errors.New("invalid patch format of setElementOrder list") 31 ErrPatchContentNotMatchRetainKeys = errors.New("patch content doesn't match retainKeys list") 32 ErrUnsupportedStrategicMergePatchFormat = errors.New("strategic merge patch format is not supported") 33 ) 34 35 func ErrNoMergeKey(m map[string]interface{}, k string) error { 36 return fmt.Errorf("map: %v does not contain declared merge key: %s", m, k) 37 } 38 39 func ErrBadArgType(expected, actual interface{}) error { 40 return fmt.Errorf("expected a %s, but received a %s", 41 reflect.TypeOf(expected), 42 reflect.TypeOf(actual)) 43 } 44 45 func ErrBadArgKind(expected, actual interface{}) error { 46 var expectedKindString, actualKindString string 47 if expected == nil { 48 expectedKindString = "nil" 49 } else { 50 expectedKindString = reflect.TypeOf(expected).Kind().String() 51 } 52 if actual == nil { 53 actualKindString = "nil" 54 } else { 55 actualKindString = reflect.TypeOf(actual).Kind().String() 56 } 57 return fmt.Errorf("expected a %s, but received a %s", expectedKindString, actualKindString) 58 } 59 60 func ErrBadPatchType(t interface{}, m map[string]interface{}) error { 61 return fmt.Errorf("unknown patch type: %s in map: %v", t, m) 62 } 63 64 // IsPreconditionFailed returns true if the provided error indicates 65 // a precondition failed. 66 func IsPreconditionFailed(err error) bool { 67 _, ok := err.(ErrPreconditionFailed) 68 return ok 69 } 70 71 type ErrPreconditionFailed struct { 72 message string 73 } 74 75 func NewErrPreconditionFailed(target map[string]interface{}) ErrPreconditionFailed { 76 s := fmt.Sprintf("precondition failed for: %v", target) 77 return ErrPreconditionFailed{s} 78 } 79 80 func (err ErrPreconditionFailed) Error() string { 81 return err.message 82 } 83 84 type ErrConflict struct { 85 message string 86 } 87 88 func NewErrConflict(patch, current string) ErrConflict { 89 s := fmt.Sprintf("patch:\n%s\nconflicts with changes made from original to current:\n%s\n", patch, current) 90 return ErrConflict{s} 91 } 92 93 func (err ErrConflict) Error() string { 94 return err.message 95 } 96 97 // IsConflict returns true if the provided error indicates 98 // a conflict between the patch and the current configuration. 99 func IsConflict(err error) bool { 100 _, ok := err.(ErrConflict) 101 return ok 102 }