errors.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package gin
  5. import (
  6. "bytes"
  7. "fmt"
  8. "reflect"
  9. "github.com/json-iterator/go"
  10. )
  11. var json = jsoniter.ConfigCompatibleWithStandardLibrary
  12. type ErrorType uint64
  13. const (
  14. ErrorTypeBind ErrorType = 1 << 63 // used when c.Bind() fails
  15. ErrorTypeRender ErrorType = 1 << 62 // used when c.Render() fails
  16. ErrorTypePrivate ErrorType = 1 << 0
  17. ErrorTypePublic ErrorType = 1 << 1
  18. ErrorTypeAny ErrorType = 1<<64 - 1
  19. ErrorTypeNu = 2
  20. )
  21. type Error struct {
  22. Err error
  23. Type ErrorType
  24. Meta interface{}
  25. }
  26. type errorMsgs []*Error
  27. var _ error = &Error{}
  28. func (msg *Error) SetType(flags ErrorType) *Error {
  29. msg.Type = flags
  30. return msg
  31. }
  32. func (msg *Error) SetMeta(data interface{}) *Error {
  33. msg.Meta = data
  34. return msg
  35. }
  36. func (msg *Error) JSON() interface{} {
  37. json := H{}
  38. if msg.Meta != nil {
  39. value := reflect.ValueOf(msg.Meta)
  40. switch value.Kind() {
  41. case reflect.Struct:
  42. return msg.Meta
  43. case reflect.Map:
  44. for _, key := range value.MapKeys() {
  45. json[key.String()] = value.MapIndex(key).Interface()
  46. }
  47. default:
  48. json["meta"] = msg.Meta
  49. }
  50. }
  51. if _, ok := json["error"]; !ok {
  52. json["error"] = msg.Error()
  53. }
  54. return json
  55. }
  56. // MarshalJSON implements the json.Marshaller interface
  57. func (msg *Error) MarshalJSON() ([]byte, error) {
  58. return json.Marshal(msg.JSON())
  59. }
  60. // Error implements the error interface
  61. func (msg Error) Error() string {
  62. return msg.Err.Error()
  63. }
  64. func (msg *Error) IsType(flags ErrorType) bool {
  65. return (msg.Type & flags) > 0
  66. }
  67. // ByType returns a readonly copy filtered the byte.
  68. // ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic
  69. func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
  70. if len(a) == 0 {
  71. return nil
  72. }
  73. if typ == ErrorTypeAny {
  74. return a
  75. }
  76. var result errorMsgs
  77. for _, msg := range a {
  78. if msg.IsType(typ) {
  79. result = append(result, msg)
  80. }
  81. }
  82. return result
  83. }
  84. // Last returns the last error in the slice. It returns nil if the array is empty.
  85. // Shortcut for errors[len(errors)-1]
  86. func (a errorMsgs) Last() *Error {
  87. if length := len(a); length > 0 {
  88. return a[length-1]
  89. }
  90. return nil
  91. }
  92. // Errors returns an array will all the error messages.
  93. // Example:
  94. // c.Error(errors.New("first"))
  95. // c.Error(errors.New("second"))
  96. // c.Error(errors.New("third"))
  97. // c.Errors.Errors() // == []string{"first", "second", "third"}
  98. func (a errorMsgs) Errors() []string {
  99. if len(a) == 0 {
  100. return nil
  101. }
  102. errorStrings := make([]string, len(a))
  103. for i, err := range a {
  104. errorStrings[i] = err.Error()
  105. }
  106. return errorStrings
  107. }
  108. func (a errorMsgs) JSON() interface{} {
  109. switch len(a) {
  110. case 0:
  111. return nil
  112. case 1:
  113. return a.Last().JSON()
  114. default:
  115. json := make([]interface{}, len(a))
  116. for i, err := range a {
  117. json[i] = err.JSON()
  118. }
  119. return json
  120. }
  121. }
  122. func (a errorMsgs) MarshalJSON() ([]byte, error) {
  123. return json.Marshal(a.JSON())
  124. }
  125. func (a errorMsgs) String() string {
  126. if len(a) == 0 {
  127. return ""
  128. }
  129. var buffer bytes.Buffer
  130. for i, msg := range a {
  131. fmt.Fprintf(&buffer, "Error #%02d: %s\n", (i + 1), msg.Err)
  132. if msg.Meta != nil {
  133. fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
  134. }
  135. }
  136. return buffer.String()
  137. }