errors.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 any
  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 any) *Error {
  42. msg.Meta = data
  43. return msg
  44. }
  45. // JSON creates a properly formatted JSON
  46. func (msg *Error) JSON() any {
  47. jsonData := 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. jsonData[key.String()] = value.MapIndex(key).Interface()
  56. }
  57. default:
  58. jsonData["meta"] = msg.Meta
  59. }
  60. }
  61. if _, ok := jsonData["error"]; !ok {
  62. jsonData["error"] = msg.Error()
  63. }
  64. return jsonData
  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. // Unwrap returns the wrapped error, to allow interoperability with errors.Is(), errors.As() and errors.Unwrap()
  79. func (msg *Error) Unwrap() error {
  80. return msg.Err
  81. }
  82. // ByType returns a readonly copy filtered the byte.
  83. // ie ByType(gin.ErrorTypePublic) returns a slice of errors with type=ErrorTypePublic.
  84. func (a errorMsgs) ByType(typ ErrorType) errorMsgs {
  85. if len(a) == 0 {
  86. return nil
  87. }
  88. if typ == ErrorTypeAny {
  89. return a
  90. }
  91. var result errorMsgs
  92. for _, msg := range a {
  93. if msg.IsType(typ) {
  94. result = append(result, msg)
  95. }
  96. }
  97. return result
  98. }
  99. // Last returns the last error in the slice. It returns nil if the array is empty.
  100. // Shortcut for errors[len(errors)-1].
  101. func (a errorMsgs) Last() *Error {
  102. if length := len(a); length > 0 {
  103. return a[length-1]
  104. }
  105. return nil
  106. }
  107. // Errors returns an array with all the error messages.
  108. // Example:
  109. // c.Error(errors.New("first"))
  110. // c.Error(errors.New("second"))
  111. // c.Error(errors.New("third"))
  112. // c.Errors.Errors() // == []string{"first", "second", "third"}
  113. func (a errorMsgs) Errors() []string {
  114. if len(a) == 0 {
  115. return nil
  116. }
  117. errorStrings := make([]string, len(a))
  118. for i, err := range a {
  119. errorStrings[i] = err.Error()
  120. }
  121. return errorStrings
  122. }
  123. func (a errorMsgs) JSON() any {
  124. switch length := len(a); length {
  125. case 0:
  126. return nil
  127. case 1:
  128. return a.Last().JSON()
  129. default:
  130. jsonData := make([]any, length)
  131. for i, err := range a {
  132. jsonData[i] = err.JSON()
  133. }
  134. return jsonData
  135. }
  136. }
  137. // MarshalJSON implements the json.Marshaller interface.
  138. func (a errorMsgs) MarshalJSON() ([]byte, error) {
  139. return json.Marshal(a.JSON())
  140. }
  141. func (a errorMsgs) String() string {
  142. if len(a) == 0 {
  143. return ""
  144. }
  145. var buffer strings.Builder
  146. for i, msg := range a {
  147. fmt.Fprintf(&buffer, "Error #%02d: %s\n", i+1, msg.Err)
  148. if msg.Meta != nil {
  149. fmt.Fprintf(&buffer, " Meta: %v\n", msg.Meta)
  150. }
  151. }
  152. return buffer.String()
  153. }