errors.go 4.3 KB

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