errors.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Error struct {
  21. Err error
  22. Type ErrorType
  23. Meta interface{}
  24. }
  25. type errorMsgs []*Error
  26. var _ error = &Error{}
  27. func (msg *Error) SetType(flags ErrorType) *Error {
  28. msg.Type = flags
  29. return msg
  30. }
  31. func (msg *Error) SetMeta(data interface{}) *Error {
  32. msg.Meta = data
  33. return msg
  34. }
  35. func (msg *Error) JSON() interface{} {
  36. json := H{}
  37. if msg.Meta != nil {
  38. value := reflect.ValueOf(msg.Meta)
  39. switch value.Kind() {
  40. case reflect.Struct:
  41. return msg.Meta
  42. case reflect.Map:
  43. for _, key := range value.MapKeys() {
  44. json[key.String()] = value.MapIndex(key).Interface()
  45. }
  46. default:
  47. json["meta"] = msg.Meta
  48. }
  49. }
  50. if _, ok := json["error"]; !ok {
  51. json["error"] = msg.Error()
  52. }
  53. return json
  54. }
  55. // MarshalJSON implements the json.Marshaller interface
  56. func (msg *Error) MarshalJSON() ([]byte, error) {
  57. return json.Marshal(msg.JSON())
  58. }
  59. // Implements the error interface
  60. func (msg Error) Error() string {
  61. return msg.Err.Error()
  62. }
  63. func (msg *Error) IsType(flags ErrorType) bool {
  64. return (msg.Type & flags) > 0
  65. }
  66. // Returns a readonly copy filtered the byte.
  67. // ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic
  68. func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
  69. if len(a) == 0 {
  70. return nil
  71. }
  72. if typ == ErrorTypeAny {
  73. return a
  74. }
  75. var result errorMsgs
  76. for _, msg := range a {
  77. if msg.IsType(typ) {
  78. result = append(result, msg)
  79. }
  80. }
  81. return result
  82. }
  83. // Returns the last error in the slice. It returns nil if the array is empty.
  84. // Shortcut for errors[len(errors)-1]
  85. func (a errorMsgs) Last() *Error {
  86. length := len(a)
  87. if length > 0 {
  88. return a[length-1]
  89. }
  90. return nil
  91. }
  92. // 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. }