apierr.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. package apierr
  2. import (
  3. "encoding/json"
  4. "kpt-tmr-group/pkg/xerr"
  5. common "kpt-tmr-group/proto/go/backend/common"
  6. "net/http"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func New(code common.Error_Code) *Error {
  10. return &Error{err: &common.Error{
  11. Code: code,
  12. Msg: errorMessage(code),
  13. }}
  14. }
  15. func errorMessage(code common.Error_Code) string {
  16. var errMessage string
  17. if msg, ok := common.Error_Code_name[int32(code)]; ok {
  18. errMessage = msg
  19. } else {
  20. errMessage = "INTERNAL_ERROR"
  21. }
  22. return errMessage
  23. }
  24. type Error struct {
  25. err *common.Error
  26. }
  27. func (e *Error) GetCode() common.Error_Code {
  28. return e.err.Code
  29. }
  30. func (e *Error) GetMsg() string {
  31. return e.err.Msg
  32. }
  33. func (e *Error) GetErrors() []string {
  34. return e.err.Errors
  35. }
  36. func (e *Error) Error() string {
  37. bs, _ := json.Marshal(e.err)
  38. return string(bs)
  39. }
  40. // Is 判断 err 和 e 是否相同
  41. func (e *Error) Is(err error) bool {
  42. if gotErr, ok := err.(*Error); !ok {
  43. return false
  44. } else {
  45. return e.err.Code == gotErr.err.Code
  46. }
  47. }
  48. func (e *Error) MarshalJSON() ([]byte, error) {
  49. return json.Marshal(e.err)
  50. }
  51. func (e *Error) UnmarshalJSON(data []byte) error {
  52. var commonErr common.Error
  53. if err := json.Unmarshal(data, &commonErr); err != nil {
  54. return err
  55. }
  56. e.err = &commonErr
  57. return nil
  58. }
  59. func (e *Error) WithLocaleMessage(c *gin.Context) *Error {
  60. if msg := e.GetMsg(); msg != "" {
  61. return e.SetMessage(msg)
  62. }
  63. return e
  64. }
  65. func (e *Error) SetMessage(m string) *Error {
  66. e.err.Msg = m
  67. return e
  68. }
  69. func (e *Error) SetErrors(errMessages []string) *Error {
  70. e.err.Errors = errMessages
  71. return e
  72. }
  73. // With more information
  74. func (e *Error) With(errs ...error) *Error {
  75. for _, err := range errs {
  76. e.err.Errors = append(e.err.Errors, err.Error())
  77. }
  78. return e
  79. }
  80. // WithContext return error with i18n message ?
  81. func WithContext(c *gin.Context, code common.Error_Code) *Error {
  82. return New(code).WithLocaleMessage(c)
  83. }
  84. // AbortError 用来处理多种内部错误.
  85. // 在复杂业务场景中,存在一个接口返回多种业务错误码情况,通过这个函数来统一处理
  86. func AbortError(c *gin.Context, err error) {
  87. if err == nil {
  88. return
  89. }
  90. if e, ok := xerr.Cause(err).(*Error); ok {
  91. // 取默认状态码
  92. statusCode := DefaultErrorStatusCode(e.err.Code)
  93. if !shouldIgnoreCode(statusCode) {
  94. c.Error(e)
  95. }
  96. c.AbortWithStatusJSON(statusCode, e.WithLocaleMessage(c))
  97. return
  98. }
  99. c.Error(err)
  100. c.AbortWithStatusJSON(http.StatusInternalServerError, WithContext(c, common.Error_INTERNAL_ERROR).With(err))
  101. }
  102. // AbortStatusError 用来处理多种内部错误和定制返回的 http status code.
  103. // 在复杂业务场景中,存在一个接口返回多种业务错误码情况,通过这个函数来统一处理
  104. func AbortStatusError(c *gin.Context, httpCode int, err error) {
  105. if err == nil {
  106. return
  107. }
  108. if e, ok := xerr.Cause(err).(*Error); ok {
  109. c.Error(e)
  110. c.AbortWithStatusJSON(httpCode, WithContext(c, e.err.Code))
  111. return
  112. }
  113. c.Error(err)
  114. c.AbortWithStatusJSON(httpCode, WithContext(c, common.Error_INTERNAL_ERROR).With(err))
  115. }
  116. // DefaultErrorStatusCode 返回错误码对应的默认 http status code
  117. func DefaultErrorStatusCode(code common.Error_Code) int {
  118. if statusCode, ok := errorStatusCode[int(code)]; ok {
  119. return statusCode
  120. }
  121. return http.StatusInternalServerError
  122. }
  123. // 错误对应默认返回的 http status code
  124. var errorStatusCode = map[int]int{
  125. 0: http.StatusOK,
  126. 10000: http.StatusUnauthorized,
  127. 11000: http.StatusBadRequest,
  128. 11001: http.StatusBadRequest,
  129. 11002: http.StatusBadRequest,
  130. 11003: http.StatusTooManyRequests,
  131. 11100: http.StatusBadRequest,
  132. 11200: http.StatusBadRequest,
  133. 20000: http.StatusBadRequest,
  134. 21000: http.StatusBadRequest,
  135. 22000: http.StatusBadRequest,
  136. 23000: http.StatusBadRequest,
  137. 23001: http.StatusBadRequest,
  138. 24000: http.StatusBadRequest,
  139. 24100: http.StatusBadRequest,
  140. 24101: http.StatusBadRequest,
  141. 24102: http.StatusBadRequest,
  142. 24103: http.StatusBadRequest,
  143. 24104: http.StatusBadRequest,
  144. 24400: http.StatusBadRequest,
  145. 24500: http.StatusBadRequest,
  146. 24501: http.StatusBadRequest,
  147. 24502: http.StatusBadRequest,
  148. 24520: http.StatusBadRequest,
  149. 24521: http.StatusBadRequest,
  150. 24522: http.StatusBadRequest,
  151. 24523: http.StatusBadRequest,
  152. 24524: http.StatusBadRequest,
  153. 24525: http.StatusBadRequest,
  154. 24526: http.StatusBadRequest,
  155. 24527: http.StatusBadRequest,
  156. 24528: http.StatusBadRequest,
  157. 24600: http.StatusBadRequest,
  158. 24601: http.StatusBadRequest,
  159. 24602: http.StatusBadRequest,
  160. 24603: http.StatusBadRequest,
  161. 24700: http.StatusBadRequest,
  162. 24701: http.StatusBadRequest,
  163. 24702: http.StatusBadRequest,
  164. 24704: http.StatusBadRequest,
  165. 24705: http.StatusBadRequest,
  166. 24706: http.StatusBadRequest,
  167. 24707: http.StatusBadRequest,
  168. 24800: http.StatusBadRequest,
  169. 24801: http.StatusBadRequest,
  170. 24802: http.StatusBadRequest,
  171. 24803: http.StatusBadRequest,
  172. 90000: http.StatusInternalServerError,
  173. 90100: http.StatusBadRequest,
  174. 90101: http.StatusBadRequest,
  175. 90102: http.StatusBadRequest,
  176. 91000: http.StatusInternalServerError,
  177. }