Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2015 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 storage |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | |
| 22 | "k8s.io/apimachinery/pkg/util/validation/field" |
| 23 | ) |
| 24 | |
| 25 | const ( |
| 26 | ErrCodeKeyNotFound int = iota + 1 |
| 27 | ErrCodeKeyExists |
| 28 | ErrCodeResourceVersionConflicts |
| 29 | ErrCodeInvalidObj |
| 30 | ErrCodeUnreachable |
| 31 | ) |
| 32 | |
| 33 | var errCodeToMessage = map[int]string{ |
| 34 | ErrCodeKeyNotFound: "key not found", |
| 35 | ErrCodeKeyExists: "key exists", |
| 36 | ErrCodeResourceVersionConflicts: "resource version conflicts", |
| 37 | ErrCodeInvalidObj: "invalid object", |
| 38 | ErrCodeUnreachable: "server unreachable", |
| 39 | } |
| 40 | |
| 41 | func NewKeyNotFoundError(key string, rv int64) *StorageError { |
| 42 | return &StorageError{ |
| 43 | Code: ErrCodeKeyNotFound, |
| 44 | Key: key, |
| 45 | ResourceVersion: rv, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | func NewKeyExistsError(key string, rv int64) *StorageError { |
| 50 | return &StorageError{ |
| 51 | Code: ErrCodeKeyExists, |
| 52 | Key: key, |
| 53 | ResourceVersion: rv, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func NewResourceVersionConflictsError(key string, rv int64) *StorageError { |
| 58 | return &StorageError{ |
| 59 | Code: ErrCodeResourceVersionConflicts, |
| 60 | Key: key, |
| 61 | ResourceVersion: rv, |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func NewUnreachableError(key string, rv int64) *StorageError { |
| 66 | return &StorageError{ |
| 67 | Code: ErrCodeUnreachable, |
| 68 | Key: key, |
| 69 | ResourceVersion: rv, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func NewInvalidObjError(key, msg string) *StorageError { |
| 74 | return &StorageError{ |
| 75 | Code: ErrCodeInvalidObj, |
| 76 | Key: key, |
| 77 | AdditionalErrorMsg: msg, |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | type StorageError struct { |
| 82 | Code int |
| 83 | Key string |
| 84 | ResourceVersion int64 |
| 85 | AdditionalErrorMsg string |
| 86 | } |
| 87 | |
| 88 | func (e *StorageError) Error() string { |
| 89 | return fmt.Sprintf("StorageError: %s, Code: %d, Key: %s, ResourceVersion: %d, AdditionalErrorMsg: %s", |
| 90 | errCodeToMessage[e.Code], e.Code, e.Key, e.ResourceVersion, e.AdditionalErrorMsg) |
| 91 | } |
| 92 | |
| 93 | // IsNotFound returns true if and only if err is "key" not found error. |
| 94 | func IsNotFound(err error) bool { |
| 95 | return isErrCode(err, ErrCodeKeyNotFound) |
| 96 | } |
| 97 | |
| 98 | // IsNodeExist returns true if and only if err is an node already exist error. |
| 99 | func IsNodeExist(err error) bool { |
| 100 | return isErrCode(err, ErrCodeKeyExists) |
| 101 | } |
| 102 | |
| 103 | // IsUnreachable returns true if and only if err indicates the server could not be reached. |
| 104 | func IsUnreachable(err error) bool { |
| 105 | return isErrCode(err, ErrCodeUnreachable) |
| 106 | } |
| 107 | |
| 108 | // IsConflict returns true if and only if err is a write conflict. |
| 109 | func IsConflict(err error) bool { |
| 110 | return isErrCode(err, ErrCodeResourceVersionConflicts) |
| 111 | } |
| 112 | |
| 113 | // IsInvalidObj returns true if and only if err is invalid error |
| 114 | func IsInvalidObj(err error) bool { |
| 115 | return isErrCode(err, ErrCodeInvalidObj) |
| 116 | } |
| 117 | |
| 118 | func isErrCode(err error, code int) bool { |
| 119 | if err == nil { |
| 120 | return false |
| 121 | } |
| 122 | if e, ok := err.(*StorageError); ok { |
| 123 | return e.Code == code |
| 124 | } |
| 125 | return false |
| 126 | } |
| 127 | |
| 128 | // InvalidError is generated when an error caused by invalid API object occurs |
| 129 | // in the storage package. |
| 130 | type InvalidError struct { |
| 131 | Errs field.ErrorList |
| 132 | } |
| 133 | |
| 134 | func (e InvalidError) Error() string { |
| 135 | return e.Errs.ToAggregate().Error() |
| 136 | } |
| 137 | |
| 138 | // IsInvalidError returns true if and only if err is an InvalidError. |
| 139 | func IsInvalidError(err error) bool { |
| 140 | _, ok := err.(InvalidError) |
| 141 | return ok |
| 142 | } |
| 143 | |
| 144 | func NewInvalidError(errors field.ErrorList) InvalidError { |
| 145 | return InvalidError{errors} |
| 146 | } |
| 147 | |
| 148 | // InternalError is generated when an error occurs in the storage package, i.e., |
| 149 | // not from the underlying storage backend (e.g., etcd). |
| 150 | type InternalError struct { |
| 151 | Reason string |
| 152 | } |
| 153 | |
| 154 | func (e InternalError) Error() string { |
| 155 | return e.Reason |
| 156 | } |
| 157 | |
| 158 | // IsInternalError returns true if and only if err is an InternalError. |
| 159 | func IsInternalError(err error) bool { |
| 160 | _, ok := err.(InternalError) |
| 161 | return ok |
| 162 | } |
| 163 | |
| 164 | func NewInternalError(reason string) InternalError { |
| 165 | return InternalError{reason} |
| 166 | } |
| 167 | |
| 168 | func NewInternalErrorf(format string, a ...interface{}) InternalError { |
| 169 | return InternalError{fmt.Sprintf(format, a)} |
| 170 | } |