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