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. "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. // MarshalJSON 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. if typ == ErrorTypeAny {
  75. return a
  76. }
  77. var result errorMsgs
  78. for _, msg := range a {
  79. if msg.IsType(typ) {
  80. result = append(result, msg)
  81. }
  82. }
  83. return result
  84. }
  85. // Returns the last error in the slice. It returns nil if the array is empty.
  86. // Shortcut for errors[len(errors)-1]
  87. func (a errorMsgs) Last() *Error {
  88. length := len(a)
  89. if length > 0 {
  90. return a[length-1]
  91. }
  92. return nil
  93. }
  94. // Returns an array will all the error messages.
  95. // Example:
  96. // c.Error(errors.New("first"))
  97. // c.Error(errors.New("second"))
  98. // c.Error(errors.New("third"))
  99. // c.Errors.Errors() // == []string{"first", "second", "third"}
  100. func (a errorMsgs) Errors() []string {
  101. if len(a) == 0 {
  102. return nil
  103. }
  104. errorStrings := make([]string, len(a))
  105. for i, err := range a {
  106. errorStrings[i] = err.Error()
  107. }
  108. return errorStrings
  109. }
  110. func (a errorMsgs) JSON() interface{} {
  111. switch len(a) {
  112. case 0:
  113. return nil
  114. case 1:
  115. return a.Last().JSON()
  116. default:
  117. json := make([]interface{}, len(a))
  118. for i, err := range a {
  119. json[i] = err.JSON()
  120. }
  121. return json
  122. }
  123. }
  124. func (a errorMsgs) MarshalJSON() ([]byte, error) {
  125. return json.Marshal(a.JSON())
  126. }
  127. func (a errorMsgs) String() string {
  128. if len(a) == 0 {
  129. return ""
  130. }
  131. var buffer bytes.Buffer
  132. for i, msg := range a {
  133. fmt.Fprintf(&buffer, "Error #%02d: %s\n", (i + 1), msg.Err)
  134. if msg.Meta != nil {
  135. fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
  136. }
  137. }
  138. return buffer.String()
  139. }