errors.go 3.7 KB

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