exc_enums.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package exception
  2. import stringUtils "eta_mini_ht_api/common/utils/string"
  3. type EtaError struct {
  4. ErrorCode int
  5. ErrorMsg string
  6. }
  7. func (e *EtaError) Error() string {
  8. return e.ErrorMsg
  9. }
  10. const (
  11. // SysErrCode 系统错误
  12. SysErrCode int = iota + 10000 // iota 自动递增,从 1 开始
  13. UnknownError
  14. Unauthorized
  15. )
  16. // BIZErrCode 业务错误
  17. const (
  18. BIZErrCode int = iota + 20000 // iota 自动递增,从 1 开始
  19. //短信
  20. IllegalCodeLength
  21. IllegalPhoneNumber
  22. SMSCodeGenerateFailed
  23. SendingSMSFailed
  24. SMSCodeAlreadySent
  25. SMSCodeExpired
  26. SMSCodeError
  27. GetAreaCodesFailed
  28. AnalystNotFound
  29. IllegalAreaCode
  30. MediaTypeError
  31. )
  32. // UserErrCode 用户
  33. const (
  34. UserErrCode int = iota + 30000 // iota 自动递增,从 1 开始
  35. TemplateUserNotFound
  36. TemplateUserCreateFailed
  37. GenerateTokenFailed
  38. JWTTokenDecodeFailed
  39. JWTTokenExpired
  40. JWTTokenInvalid
  41. NotCurrentUserError
  42. FeedBackMsgEmpty
  43. FeedBackError
  44. IllegalFollowType
  45. UserFollowAnalystFailed
  46. GetNoticeFileError
  47. GetDisclaimerFileError
  48. )
  49. // WechatErrCode 微信
  50. const (
  51. WechatErrCode int = iota + 40000 // iota 自动递增,从 1 开始
  52. WeChatServerError
  53. WeChatResponseError
  54. WechatUserInfoFailed
  55. WeChatCodeEmpty
  56. WeChatIllegalRequest
  57. )
  58. const (
  59. ReportErrCode int = iota + 50000 // iota 自动递增,从 1 开始
  60. GetPublishedRandListFailed
  61. GetPermissionListFailed
  62. ReportRecordClickCountFailed
  63. GetHotRandListFailed
  64. QueryReportPageFailed
  65. SearchReportPageFailed
  66. GetReportFailed
  67. SearchKeyEmptyError
  68. )
  69. // ErrorMap 用于存储错误码和错误信息的映射
  70. var ErrorMap = map[int]string{
  71. UnknownError: "未知错误",
  72. Unauthorized: "用户未授权",
  73. IllegalCodeLength: "无效的验证码位数设置",
  74. IllegalPhoneNumber: "无效的手机号码",
  75. SMSCodeGenerateFailed: "生成手机验证码失败",
  76. SendingSMSFailed: "发送手机验证码失败",
  77. SMSCodeAlreadySent: "当前手机验证码已发送,请稍后再试",
  78. SMSCodeExpired: "验证码已过期",
  79. SMSCodeError: "验证码错误",
  80. AnalystNotFound: "研究员不存在",
  81. GetAreaCodesFailed: "获取手机区号失败",
  82. IllegalAreaCode: "无效的区号",
  83. MediaTypeError: "媒体类型非法",
  84. //用户
  85. TemplateUserNotFound: "临时用户记录不存在",
  86. TemplateUserCreateFailed: "创建临时用户失败",
  87. GenerateTokenFailed: "创建token失败",
  88. JWTTokenDecodeFailed: "token解析失败",
  89. JWTTokenExpired: "token已过期",
  90. JWTTokenInvalid: "token无效",
  91. NotCurrentUserError: "用户信息不一致,非当前手机用户操作",
  92. FeedBackMsgEmpty: "反馈信息不能为空",
  93. FeedBackError: "提交反馈信息失败",
  94. IllegalFollowType: "无效的关注类型",
  95. UserFollowAnalystFailed: "关注研究员失败",
  96. GetNoticeFileError: "获取注册须知失败",
  97. GetDisclaimerFileError: "获取免责声明失败",
  98. //微信
  99. WeChatServerError: "微信服务器发生错误",
  100. WechatUserInfoFailed: "获取微信用户信息失败",
  101. WeChatResponseError: "解析微信响应数据失败",
  102. WeChatCodeEmpty: "微信获取用户信息,code不能为空",
  103. WeChatIllegalRequest: "不合法的微信请求",
  104. //研报
  105. GetPublishedRandListFailed: "获取已发布研报列表失败",
  106. GetPermissionListFailed: "获取品种列表失败",
  107. ReportRecordClickCountFailed: "添加点击访问次数失败",
  108. GetHotRandListFailed: "获取热门研报列表失败",
  109. QueryReportPageFailed: "分页查询报告列表失败",
  110. SearchReportPageFailed: "分页搜索报告列表失败",
  111. GetReportFailed: "获取研报详情失败",
  112. SearchKeyEmptyError: "搜索关键字不能为空",
  113. }
  114. func Equals(code int, message string) bool {
  115. if stringUtils.IsEmptyOrNil(message) {
  116. return false
  117. }
  118. return ErrorMap[code] == message
  119. }
  120. func GetMsg(code int) string {
  121. return ErrorMap[code]
  122. }
  123. func newException(code int, msg string) error {
  124. return &EtaError{
  125. ErrorCode: code,
  126. ErrorMsg: msg,
  127. }
  128. }
  129. func New(code int) *EtaError {
  130. err := ErrorMap[code]
  131. if stringUtils.IsBlank(err) {
  132. return newException(UnknownError, ErrorMap[UnknownError]).(*EtaError)
  133. }
  134. return newException(code, err).(*EtaError)
  135. }