You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

errors.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2014 Martini Authors
  2. // Copyright 2014 The Macaron Authors
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  5. // not use this file except in compliance with the License. You may obtain
  6. // 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, WITHOUT
  12. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. // License for the specific language governing permissions and limitations
  14. // under the License.
  15. package binding
  16. const (
  17. // Type mismatch errors.
  18. ERR_CONTENT_TYPE = "ContentTypeError"
  19. ERR_DESERIALIZATION = "DeserializationError"
  20. ERR_INTERGER_TYPE = "IntegerTypeError"
  21. ERR_BOOLEAN_TYPE = "BooleanTypeError"
  22. ERR_FLOAT_TYPE = "FloatTypeError"
  23. // Validation errors.
  24. ERR_REQUIRED = "RequiredError"
  25. ERR_ALPHA_DASH = "AlphaDashError"
  26. ERR_ALPHA_DASH_DOT = "AlphaDashDotError"
  27. ERR_SIZE = "SizeError"
  28. ERR_MIN_SIZE = "MinSizeError"
  29. ERR_MAX_SIZE = "MaxSizeError"
  30. ERR_RANGE = "RangeError"
  31. ERR_EMAIL = "EmailError"
  32. ERR_URL = "UrlError"
  33. ERR_IN = "InError"
  34. ERR_NOT_INT = "NotInError"
  35. ERR_INCLUDE = "IncludeError"
  36. ERR_EXCLUDE = "ExcludeError"
  37. ERR_DEFAULT = "DefaultError"
  38. )
  39. type (
  40. // Errors may be generated during deserialization, binding,
  41. // or validation. This type is mapped to the context so you
  42. // can inject it into your own handlers and use it in your
  43. // application if you want all your errors to look the same.
  44. Errors []Error
  45. Error struct {
  46. // An error supports zero or more field names, because an
  47. // error can morph three ways: (1) it can indicate something
  48. // wrong with the request as a whole, (2) it can point to a
  49. // specific problem with a particular input field, or (3) it
  50. // can span multiple related input fields.
  51. FieldNames []string `json:"fieldNames,omitempty"`
  52. // The classification is like an error code, convenient to
  53. // use when processing or categorizing an error programmatically.
  54. // It may also be called the "kind" of error.
  55. Classification string `json:"classification,omitempty"`
  56. // Message should be human-readable and detailed enough to
  57. // pinpoint and resolve the problem, but it should be brief. For
  58. // example, a payload of 100 objects in a JSON array might have
  59. // an error in the 41st object. The message should help the
  60. // end user find and fix the error with their request.
  61. Message string `json:"message,omitempty"`
  62. }
  63. )
  64. // Add adds an error associated with the fields indicated
  65. // by fieldNames, with the given classification and message.
  66. func (e *Errors) Add(fieldNames []string, classification, message string) {
  67. *e = append(*e, Error{
  68. FieldNames: fieldNames,
  69. Classification: classification,
  70. Message: message,
  71. })
  72. }
  73. // Len returns the number of errors.
  74. func (e *Errors) Len() int {
  75. return len(*e)
  76. }
  77. // Has determines whether an Errors slice has an Error with
  78. // a given classification in it; it does not search on messages
  79. // or field names.
  80. func (e *Errors) Has(class string) bool {
  81. for _, err := range *e {
  82. if err.Kind() == class {
  83. return true
  84. }
  85. }
  86. return false
  87. }
  88. /*
  89. // WithClass gets a copy of errors that are classified by the
  90. // the given classification.
  91. func (e *Errors) WithClass(classification string) Errors {
  92. var errs Errors
  93. for _, err := range *e {
  94. if err.Kind() == classification {
  95. errs = append(errs, err)
  96. }
  97. }
  98. return errs
  99. }
  100. // ForField gets a copy of errors that are associated with the
  101. // field by the given name.
  102. func (e *Errors) ForField(name string) Errors {
  103. var errs Errors
  104. for _, err := range *e {
  105. for _, fieldName := range err.Fields() {
  106. if fieldName == name {
  107. errs = append(errs, err)
  108. break
  109. }
  110. }
  111. }
  112. return errs
  113. }
  114. // Get gets errors of a particular class for the specified
  115. // field name.
  116. func (e *Errors) Get(class, fieldName string) Errors {
  117. var errs Errors
  118. for _, err := range *e {
  119. if err.Kind() == class {
  120. for _, nameOfField := range err.Fields() {
  121. if nameOfField == fieldName {
  122. errs = append(errs, err)
  123. break
  124. }
  125. }
  126. }
  127. }
  128. return errs
  129. }
  130. */
  131. // Fields returns the list of field names this error is
  132. // associated with.
  133. func (e Error) Fields() []string {
  134. return e.FieldNames
  135. }
  136. // Kind returns this error's classification.
  137. func (e Error) Kind() string {
  138. return e.Classification
  139. }
  140. // Error returns this error's message.
  141. func (e Error) Error() string {
  142. return e.Message
  143. }