Debian Codegraph @

pkg/storage/errors.go

raw
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 "errors"
21 "fmt"
22
23 apierrors "k8s.io/apimachinery/pkg/api/errors"
24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25 "k8s.io/apimachinery/pkg/util/validation/field"
26 )
27
28 var (
29 ErrResourceVersionSetOnCreate = errors.New("resourceVersion should not be set on objects to be created")
30 ErrStorageNotReady = errors.New("storage not ready")
31 )
32
33 const (
34 ErrCodeKeyNotFound int = iota + 1
35 ErrCodeKeyExists
36 ErrCodeResourceVersionConflicts
37 ErrCodeInvalidObj
38 ErrCodeUnreachable
39 ErrCodeTimeout
40 ErrCodeCorruptObj
41 )
42
43 var errCodeToMessage = map[int]string{
44 ErrCodeKeyNotFound: "key not found",
45 ErrCodeKeyExists: "key exists",
46 ErrCodeResourceVersionConflicts: "resource version conflicts",
47 ErrCodeInvalidObj: "invalid object",
48 ErrCodeUnreachable: "server unreachable",
49 ErrCodeTimeout: "request timeout",
50 ErrCodeCorruptObj: "corrupt object",
51 }
52
53 func NewKeyNotFoundError(key string, rv int64) *StorageError {
54 return &StorageError{
55 Code: ErrCodeKeyNotFound,
56 Key: key,
57 ResourceVersion: rv,
58 }
59 }
60
61 func NewKeyExistsError(key string, rv int64) *StorageError {
62 return &StorageError{
63 Code: ErrCodeKeyExists,
64 Key: key,
65 ResourceVersion: rv,
66 }
67 }
68
69 func NewResourceVersionConflictsError(key string, rv int64) *StorageError {
70 return &StorageError{
71 Code: ErrCodeResourceVersionConflicts,
72 Key: key,
73 ResourceVersion: rv,
74 }
75 }
76
77 func NewUnreachableError(key string, rv int64) *StorageError {
78 return &StorageError{
79 Code: ErrCodeUnreachable,
80 Key: key,
81 ResourceVersion: rv,
82 }
83 }
84
85 func NewTimeoutError(key, msg string) *StorageError {
86 return &StorageError{
87 Code: ErrCodeTimeout,
88 Key: key,
89 err: errors.New(msg),
90 }
91 }
92
93 func NewInvalidObjError(key, msg string) *StorageError {
94 return &StorageError{
95 Code: ErrCodeInvalidObj,
96 Key: key,
97 err: errors.New(msg),
98 }
99 }
100
101 // NewCorruptObjError returns a new StorageError, it represents a corrupt object:
102 // a) object data retrieved from the storage failed to transform with the given err.
103 // b) the given object failed to decode with the given err
104 func NewCorruptObjError(key string, err error) *StorageError {
105 return &StorageError{
106 Code: ErrCodeCorruptObj,
107 Key: key,
108 err: err,
109 }
110 }
111
112 type StorageError struct {
113 Code int
114 Key string
115 ResourceVersion int64
116
117 // inner error
118 err error
119 }
120
121 func (e *StorageError) Unwrap() error { return e.err }
122
123 func (e *StorageError) Error() string {
124 return fmt.Sprintf("StorageError: %s, Code: %d, Key: %s, ResourceVersion: %d, AdditionalErrorMsg: %v",
125 errCodeToMessage[e.Code], e.Code, e.Key, e.ResourceVersion, e.err)
126 }
127
128 // IsNotFound returns true if and only if err is "key" not found error.
129 func IsNotFound(err error) bool {
130 return isErrCode(err, ErrCodeKeyNotFound)
131 }
132
133 // IsExist returns true if and only if err is "key" already exists error.
134 func IsExist(err error) bool {
135 return isErrCode(err, ErrCodeKeyExists)
136 }
137
138 // IsUnreachable returns true if and only if err indicates the server could not be reached.
139 func IsUnreachable(err error) bool {
140 return isErrCode(err, ErrCodeUnreachable)
141 }
142
143 // IsConflict returns true if and only if err is a write conflict.
144 func IsConflict(err error) bool {
145 return isErrCode(err, ErrCodeResourceVersionConflicts)
146 }
147
148 // IsRequestTimeout returns true if and only if err indicates that the request has timed out.
149 func IsRequestTimeout(err error) bool {
150 return isErrCode(err, ErrCodeTimeout)
151 }
152
153 // IsInvalidObj returns true if and only if err is invalid error
154 func IsInvalidObj(err error) bool {
155 return isErrCode(err, ErrCodeInvalidObj)
156 }
157
158 // IsCorruptObject returns true if and only if:
159 // a) the given object data retrieved from the storage is not transformable, or
160 // b) the given object failed to decode properly
161 func IsCorruptObject(err error) bool {
162 if err == nil {
163 return false
164 }
165 var storageErr *StorageError
166 if !errors.As(err, &storageErr) {
167 return false
168 }
169
170 return storageErr.Code == ErrCodeCorruptObj
171 }
172
173 func isErrCode(err error, code int) bool {
174 if err == nil {
175 return false
176 }
177 if e, ok := err.(*StorageError); ok {
178 return e.Code == code
179 }
180 return false
181 }
182
183 // InvalidError is generated when an error caused by invalid API object occurs
184 // in the storage package.
185 type InvalidError struct {
186 Errs field.ErrorList
187 }
188
189 func (e InvalidError) Error() string {
190 return e.Errs.ToAggregate().Error()
191 }
192
193 // IsInvalidError returns true if and only if err is an InvalidError.
194 func IsInvalidError(err error) bool {
195 _, ok := err.(InvalidError)
196 return ok
197 }
198
199 func NewInvalidError(errors field.ErrorList) InvalidError {
200 return InvalidError{errors}
201 }
202
203 // InternalError is generated when an error occurs in the storage package, i.e.,
204 // not from the underlying storage backend (e.g., etcd).
205 type InternalError struct {
206 Reason string
207
208 // retain the inner error to maintain the error tree, so as to enable us
209 // to do proper error checking, but we also need to be backward compatible.
210 err error
211 }
212
213 func (e InternalError) Error() string {
214 return e.Reason
215 }
216
217 func (e InternalError) Unwrap() error { return e.err }
218
219 // IsInternalError returns true if and only if err is an InternalError.
220 func IsInternalError(err error) bool {
221 _, ok := err.(InternalError)
222 return ok
223 }
224
225 func NewInternalError(err error) InternalError {
226 return InternalError{Reason: err.Error(), err: err}
227 }
228
229 var tooLargeResourceVersionCauseMsg = "Too large resource version"
230
231 // NewTooLargeResourceVersionError returns a timeout error with the given retrySeconds for a request for
232 // a minimum resource version that is larger than the largest currently available resource version for a requested resource.
233 func NewTooLargeResourceVersionError(minimumResourceVersion, currentRevision uint64, retrySeconds int) error {
234 err := apierrors.NewTimeoutError(fmt.Sprintf("Too large resource version: %d, current: %d", minimumResourceVersion, currentRevision), retrySeconds)
235 err.ErrStatus.Details.Causes = []metav1.StatusCause{
236 {
237 Type: metav1.CauseTypeResourceVersionTooLarge,
238 Message: tooLargeResourceVersionCauseMsg,
239 },
240 }
241 return err
242 }
243
244 // IsTooLargeResourceVersion returns true if the error is a TooLargeResourceVersion error.
245 func IsTooLargeResourceVersion(err error) bool {
246 if !apierrors.IsTimeout(err) {
247 return false
248 }
249 return apierrors.HasStatusCause(err, metav1.CauseTypeResourceVersionTooLarge)
250 }