blob: a4d134ac991607fcb1a5ae2e3c3e365f2ab7fb09 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package storage
18
19import (
20 "fmt"
21
22 "k8s.io/apimachinery/pkg/util/validation/field"
23)
24
25const (
26 ErrCodeKeyNotFound int = iota + 1
27 ErrCodeKeyExists
28 ErrCodeResourceVersionConflicts
29 ErrCodeInvalidObj
30 ErrCodeUnreachable
31)
32
33var 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
41func NewKeyNotFoundError(key string, rv int64) *StorageError {
42 return &StorageError{
43 Code: ErrCodeKeyNotFound,
44 Key: key,
45 ResourceVersion: rv,
46 }
47}
48
49func NewKeyExistsError(key string, rv int64) *StorageError {
50 return &StorageError{
51 Code: ErrCodeKeyExists,
52 Key: key,
53 ResourceVersion: rv,
54 }
55}
56
57func NewResourceVersionConflictsError(key string, rv int64) *StorageError {
58 return &StorageError{
59 Code: ErrCodeResourceVersionConflicts,
60 Key: key,
61 ResourceVersion: rv,
62 }
63}
64
65func NewUnreachableError(key string, rv int64) *StorageError {
66 return &StorageError{
67 Code: ErrCodeUnreachable,
68 Key: key,
69 ResourceVersion: rv,
70 }
71}
72
73func NewInvalidObjError(key, msg string) *StorageError {
74 return &StorageError{
75 Code: ErrCodeInvalidObj,
76 Key: key,
77 AdditionalErrorMsg: msg,
78 }
79}
80
81type StorageError struct {
82 Code int
83 Key string
84 ResourceVersion int64
85 AdditionalErrorMsg string
86}
87
88func (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.
94func 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.
99func 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.
104func IsUnreachable(err error) bool {
105 return isErrCode(err, ErrCodeUnreachable)
106}
107
108// IsConflict returns true if and only if err is a write conflict.
109func IsConflict(err error) bool {
110 return isErrCode(err, ErrCodeResourceVersionConflicts)
111}
112
113// IsInvalidObj returns true if and only if err is invalid error
114func IsInvalidObj(err error) bool {
115 return isErrCode(err, ErrCodeInvalidObj)
116}
117
118func 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.
130type InvalidError struct {
131 Errs field.ErrorList
132}
133
134func (e InvalidError) Error() string {
135 return e.Errs.ToAggregate().Error()
136}
137
138// IsInvalidError returns true if and only if err is an InvalidError.
139func IsInvalidError(err error) bool {
140 _, ok := err.(InvalidError)
141 return ok
142}
143
144func 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).
150type InternalError struct {
151 Reason string
152}
153
154func (e InternalError) Error() string {
155 return e.Reason
156}
157
158// IsInternalError returns true if and only if err is an InternalError.
159func IsInternalError(err error) bool {
160 _, ok := err.(InternalError)
161 return ok
162}
163
164func NewInternalError(reason string) InternalError {
165 return InternalError{reason}
166}
167
168func NewInternalErrorf(format string, a ...interface{}) InternalError {
169 return InternalError{fmt.Sprintf(format, a)}
170}