另客网go项目公用的代码库

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. "fmt"
  8. "reflect"
  9. "github.com/gin-gonic/gin/json"
  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. // Error 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. // ByType 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. // Last 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. if length := len(a); length > 0 {
  87. return a[length-1]
  88. }
  89. return nil
  90. }
  91. // Errors returns an array will all the error messages.
  92. // Example:
  93. // c.Error(errors.New("first"))
  94. // c.Error(errors.New("second"))
  95. // c.Error(errors.New("third"))
  96. // c.Errors.Errors() // == []string{"first", "second", "third"}
  97. func (a errorMsgs) Errors() []string {
  98. if len(a) == 0 {
  99. return nil
  100. }
  101. errorStrings := make([]string, len(a))
  102. for i, err := range a {
  103. errorStrings[i] = err.Error()
  104. }
  105. return errorStrings
  106. }
  107. func (a errorMsgs) JSON() interface{} {
  108. switch len(a) {
  109. case 0:
  110. return nil
  111. case 1:
  112. return a.Last().JSON()
  113. default:
  114. json := make([]interface{}, len(a))
  115. for i, err := range a {
  116. json[i] = err.JSON()
  117. }
  118. return json
  119. }
  120. }
  121. func (a errorMsgs) MarshalJSON() ([]byte, error) {
  122. return json.Marshal(a.JSON())
  123. }
  124. func (a errorMsgs) String() string {
  125. if len(a) == 0 {
  126. return ""
  127. }
  128. var buffer bytes.Buffer
  129. for i, msg := range a {
  130. fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)
  131. if msg.Meta != nil {
  132. fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
  133. }
  134. }
  135. return buffer.String()
  136. }