errors.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package binding
  2. type (
  3. // Errors may be generated during deserialization, binding,
  4. // or validation. This type is mapped to the context so you
  5. // can inject it into your own handlers and use it in your
  6. // application if you want all your errors to look the same.
  7. Errors []Error
  8. Error struct {
  9. // An error supports zero or more field names, because an
  10. // error can morph three ways: (1) it can indicate something
  11. // wrong with the request as a whole, (2) it can point to a
  12. // specific problem with a particular input field, or (3) it
  13. // can span multiple related input fields.
  14. FieldNames []string `json:"fieldNames,omitempty"`
  15. // The classification is like an error code, convenient to
  16. // use when processing or categorizing an error programmatically.
  17. // It may also be called the "kind" of error.
  18. Classification string `json:"classification,omitempty"`
  19. // Message should be human-readable and detailed enough to
  20. // pinpoint and resolve the problem, but it should be brief. For
  21. // example, a payload of 100 objects in a JSON array might have
  22. // an error in the 41st object. The message should help the
  23. // end user find and fix the error with their request.
  24. Message string `json:"message,omitempty"`
  25. }
  26. )
  27. // Add adds an error associated with the fields indicated
  28. // by fieldNames, with the given classification and message.
  29. func (e *Errors) Add(fieldNames []string, classification, message string) {
  30. *e = append(*e, Error{
  31. FieldNames: fieldNames,
  32. Classification: classification,
  33. Message: message,
  34. })
  35. }
  36. // Len returns the number of errors.
  37. func (e *Errors) Len() int {
  38. return len(*e)
  39. }
  40. // Has determines whether an Errors slice has an Error with
  41. // a given classification in it; it does not search on messages
  42. // or field names.
  43. func (e *Errors) Has(class string) bool {
  44. for _, err := range *e {
  45. if err.Kind() == class {
  46. return true
  47. }
  48. }
  49. return false
  50. }
  51. /*
  52. // WithClass gets a copy of errors that are classified by the
  53. // the given classification.
  54. func (e *Errors) WithClass(classification string) Errors {
  55. var errs Errors
  56. for _, err := range *e {
  57. if err.Kind() == classification {
  58. errs = append(errs, err)
  59. }
  60. }
  61. return errs
  62. }
  63. // ForField gets a copy of errors that are associated with the
  64. // field by the given name.
  65. func (e *Errors) ForField(name string) Errors {
  66. var errs Errors
  67. for _, err := range *e {
  68. for _, fieldName := range err.Fields() {
  69. if fieldName == name {
  70. errs = append(errs, err)
  71. break
  72. }
  73. }
  74. }
  75. return errs
  76. }
  77. // Get gets errors of a particular class for the specified
  78. // field name.
  79. func (e *Errors) Get(class, fieldName string) Errors {
  80. var errs Errors
  81. for _, err := range *e {
  82. if err.Kind() == class {
  83. for _, nameOfField := range err.Fields() {
  84. if nameOfField == fieldName {
  85. errs = append(errs, err)
  86. break
  87. }
  88. }
  89. }
  90. }
  91. return errs
  92. }
  93. */
  94. // Fields returns the list of field names this error is
  95. // associated with.
  96. func (e Error) Fields() []string {
  97. return e.FieldNames
  98. }
  99. // Kind returns this error's classification.
  100. func (e Error) Kind() string {
  101. return e.Classification
  102. }
  103. // Error returns this error's message.
  104. func (e Error) Error() string {
  105. return e.Message
  106. }
  107. const (
  108. RequiredError = "RequiredError"
  109. ContentTypeError = "ContentTypeError"
  110. DeserializationError = "DeserializationError"
  111. TypeError = "TypeError"
  112. )