errors.go 3.1 KB

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