errors.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. Copyright 2014 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 field
  14. import (
  15. "fmt"
  16. "reflect"
  17. "strconv"
  18. "strings"
  19. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  20. "k8s.io/apimachinery/pkg/util/sets"
  21. )
  22. // Error is an implementation of the 'error' interface, which represents a
  23. // field-level validation error.
  24. type Error struct {
  25. Type ErrorType
  26. Field string
  27. BadValue interface{}
  28. Detail string
  29. }
  30. var _ error = &Error{}
  31. // Error implements the error interface.
  32. func (v *Error) Error() string {
  33. return fmt.Sprintf("%s: %s", v.Field, v.ErrorBody())
  34. }
  35. // ErrorBody returns the error message without the field name. This is useful
  36. // for building nice-looking higher-level error reporting.
  37. func (v *Error) ErrorBody() string {
  38. var s string
  39. switch v.Type {
  40. case ErrorTypeRequired, ErrorTypeForbidden, ErrorTypeTooLong, ErrorTypeInternal:
  41. s = fmt.Sprintf("%s", v.Type)
  42. default:
  43. value := v.BadValue
  44. valueType := reflect.TypeOf(value)
  45. if value == nil || valueType == nil {
  46. value = "null"
  47. } else if valueType.Kind() == reflect.Ptr {
  48. if reflectValue := reflect.ValueOf(value); reflectValue.IsNil() {
  49. value = "null"
  50. } else {
  51. value = reflectValue.Elem().Interface()
  52. }
  53. }
  54. switch t := value.(type) {
  55. case int64, int32, float64, float32, bool:
  56. // use simple printer for simple types
  57. s = fmt.Sprintf("%s: %v", v.Type, value)
  58. case string:
  59. s = fmt.Sprintf("%s: %q", v.Type, t)
  60. case fmt.Stringer:
  61. // anything that defines String() is better than raw struct
  62. s = fmt.Sprintf("%s: %s", v.Type, t.String())
  63. default:
  64. // fallback to raw struct
  65. // TODO: internal types have panic guards against json.Marshalling to prevent
  66. // accidental use of internal types in external serialized form. For now, use
  67. // %#v, although it would be better to show a more expressive output in the future
  68. s = fmt.Sprintf("%s: %#v", v.Type, value)
  69. }
  70. }
  71. if len(v.Detail) != 0 {
  72. s += fmt.Sprintf(": %s", v.Detail)
  73. }
  74. return s
  75. }
  76. // ErrorType is a machine readable value providing more detail about why
  77. // a field is invalid. These values are expected to match 1-1 with
  78. // CauseType in api/types.go.
  79. type ErrorType string
  80. // TODO: These values are duplicated in api/types.go, but there's a circular dep. Fix it.
  81. const (
  82. // ErrorTypeNotFound is used to report failure to find a requested value
  83. // (e.g. looking up an ID). See NotFound().
  84. ErrorTypeNotFound ErrorType = "FieldValueNotFound"
  85. // ErrorTypeRequired is used to report required values that are not
  86. // provided (e.g. empty strings, null values, or empty arrays). See
  87. // Required().
  88. ErrorTypeRequired ErrorType = "FieldValueRequired"
  89. // ErrorTypeDuplicate is used to report collisions of values that must be
  90. // unique (e.g. unique IDs). See Duplicate().
  91. ErrorTypeDuplicate ErrorType = "FieldValueDuplicate"
  92. // ErrorTypeInvalid is used to report malformed values (e.g. failed regex
  93. // match, too long, out of bounds). See Invalid().
  94. ErrorTypeInvalid ErrorType = "FieldValueInvalid"
  95. // ErrorTypeNotSupported is used to report unknown values for enumerated
  96. // fields (e.g. a list of valid values). See NotSupported().
  97. ErrorTypeNotSupported ErrorType = "FieldValueNotSupported"
  98. // ErrorTypeForbidden is used to report valid (as per formatting rules)
  99. // values which would be accepted under some conditions, but which are not
  100. // permitted by the current conditions (such as security policy). See
  101. // Forbidden().
  102. ErrorTypeForbidden ErrorType = "FieldValueForbidden"
  103. // ErrorTypeTooLong is used to report that the given value is too long.
  104. // This is similar to ErrorTypeInvalid, but the error will not include the
  105. // too-long value. See TooLong().
  106. ErrorTypeTooLong ErrorType = "FieldValueTooLong"
  107. // ErrorTypeInternal is used to report other errors that are not related
  108. // to user input. See InternalError().
  109. ErrorTypeInternal ErrorType = "InternalError"
  110. )
  111. // String converts a ErrorType into its corresponding canonical error message.
  112. func (t ErrorType) String() string {
  113. switch t {
  114. case ErrorTypeNotFound:
  115. return "Not found"
  116. case ErrorTypeRequired:
  117. return "Required value"
  118. case ErrorTypeDuplicate:
  119. return "Duplicate value"
  120. case ErrorTypeInvalid:
  121. return "Invalid value"
  122. case ErrorTypeNotSupported:
  123. return "Unsupported value"
  124. case ErrorTypeForbidden:
  125. return "Forbidden"
  126. case ErrorTypeTooLong:
  127. return "Too long"
  128. case ErrorTypeInternal:
  129. return "Internal error"
  130. default:
  131. panic(fmt.Sprintf("unrecognized validation error: %q", string(t)))
  132. }
  133. }
  134. // NotFound returns a *Error indicating "value not found". This is
  135. // used to report failure to find a requested value (e.g. looking up an ID).
  136. func NotFound(field *Path, value interface{}) *Error {
  137. return &Error{ErrorTypeNotFound, field.String(), value, ""}
  138. }
  139. // Required returns a *Error indicating "value required". This is used
  140. // to report required values that are not provided (e.g. empty strings, null
  141. // values, or empty arrays).
  142. func Required(field *Path, detail string) *Error {
  143. return &Error{ErrorTypeRequired, field.String(), "", detail}
  144. }
  145. // Duplicate returns a *Error indicating "duplicate value". This is
  146. // used to report collisions of values that must be unique (e.g. names or IDs).
  147. func Duplicate(field *Path, value interface{}) *Error {
  148. return &Error{ErrorTypeDuplicate, field.String(), value, ""}
  149. }
  150. // Invalid returns a *Error indicating "invalid value". This is used
  151. // to report malformed values (e.g. failed regex match, too long, out of bounds).
  152. func Invalid(field *Path, value interface{}, detail string) *Error {
  153. return &Error{ErrorTypeInvalid, field.String(), value, detail}
  154. }
  155. // NotSupported returns a *Error indicating "unsupported value".
  156. // This is used to report unknown values for enumerated fields (e.g. a list of
  157. // valid values).
  158. func NotSupported(field *Path, value interface{}, validValues []string) *Error {
  159. detail := ""
  160. if validValues != nil && len(validValues) > 0 {
  161. quotedValues := make([]string, len(validValues))
  162. for i, v := range validValues {
  163. quotedValues[i] = strconv.Quote(v)
  164. }
  165. detail = "supported values: " + strings.Join(quotedValues, ", ")
  166. }
  167. return &Error{ErrorTypeNotSupported, field.String(), value, detail}
  168. }
  169. // Forbidden returns a *Error indicating "forbidden". This is used to
  170. // report valid (as per formatting rules) values which would be accepted under
  171. // some conditions, but which are not permitted by current conditions (e.g.
  172. // security policy).
  173. func Forbidden(field *Path, detail string) *Error {
  174. return &Error{ErrorTypeForbidden, field.String(), "", detail}
  175. }
  176. // TooLong returns a *Error indicating "too long". This is used to
  177. // report that the given value is too long. This is similar to
  178. // Invalid, but the returned error will not include the too-long
  179. // value.
  180. func TooLong(field *Path, value interface{}, maxLength int) *Error {
  181. return &Error{ErrorTypeTooLong, field.String(), value, fmt.Sprintf("must have at most %d characters", maxLength)}
  182. }
  183. // InternalError returns a *Error indicating "internal error". This is used
  184. // to signal that an error was found that was not directly related to user
  185. // input. The err argument must be non-nil.
  186. func InternalError(field *Path, err error) *Error {
  187. return &Error{ErrorTypeInternal, field.String(), nil, err.Error()}
  188. }
  189. // ErrorList holds a set of Errors. It is plausible that we might one day have
  190. // non-field errors in this same umbrella package, but for now we don't, so
  191. // we can keep it simple and leave ErrorList here.
  192. type ErrorList []*Error
  193. // NewErrorTypeMatcher returns an errors.Matcher that returns true
  194. // if the provided error is a Error and has the provided ErrorType.
  195. func NewErrorTypeMatcher(t ErrorType) utilerrors.Matcher {
  196. return func(err error) bool {
  197. if e, ok := err.(*Error); ok {
  198. return e.Type == t
  199. }
  200. return false
  201. }
  202. }
  203. // ToAggregate converts the ErrorList into an errors.Aggregate.
  204. func (list ErrorList) ToAggregate() utilerrors.Aggregate {
  205. errs := make([]error, 0, len(list))
  206. errorMsgs := sets.NewString()
  207. for _, err := range list {
  208. msg := fmt.Sprintf("%v", err)
  209. if errorMsgs.Has(msg) {
  210. continue
  211. }
  212. errorMsgs.Insert(msg)
  213. errs = append(errs, err)
  214. }
  215. return utilerrors.NewAggregate(errs)
  216. }
  217. func fromAggregate(agg utilerrors.Aggregate) ErrorList {
  218. errs := agg.Errors()
  219. list := make(ErrorList, len(errs))
  220. for i := range errs {
  221. list[i] = errs[i].(*Error)
  222. }
  223. return list
  224. }
  225. // Filter removes items from the ErrorList that match the provided fns.
  226. func (list ErrorList) Filter(fns ...utilerrors.Matcher) ErrorList {
  227. err := utilerrors.FilterOut(list.ToAggregate(), fns...)
  228. if err == nil {
  229. return nil
  230. }
  231. // FilterOut takes an Aggregate and returns an Aggregate
  232. return fromAggregate(err.(utilerrors.Aggregate))
  233. }