errors.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Package msg
  2. // 一个错误编号表示唯一一个错误发生地点
  3. // 错误编号格式:顺序号*1000000 + 程序编号*1000 + 模块编号
  4. // 模块内函数发生错误,编号:1-100
  5. // 特定错误,编号:101-200
  6. // 其他地方发生错误,占用地址列表
  7. // 1、
  8. package msg
  9. import "fmt"
  10. // ErrorCode 结构体表示错误编码
  11. type ErrorCode struct {
  12. ProgramID int
  13. ModuleID int
  14. }
  15. const (
  16. ServerTypeLogin = 1
  17. ServerTypeGate = 2
  18. ServerTypeWorld = 3
  19. ServerTypeAll = 999 // 所有服务器
  20. )
  21. const (
  22. GateModleGame = 1
  23. GateModleGate = 2
  24. GateModlePush = 3
  25. WorldModleDb = 1
  26. WorldModleGame = 2
  27. WorldModleGate = 3
  28. WorldModleSystemData = 4
  29. AllModelAll = 999 // 所有模块
  30. )
  31. // 对象类型 1000种
  32. const (
  33. ErrObjectParam = 1
  34. ErrObjectPlayer = 2
  35. ErrObjectConfig = 3
  36. ErrObjectCondition = 4
  37. ErrObjectTarget = 5
  38. ErrObjectItem = 6
  39. ErrObjectResult = 7
  40. ErrObjectHero = 9
  41. ErrObjectTreasures = 10
  42. ErrObjectChapter = 11
  43. ErrObjectReward = 12
  44. ErrObjectSystem = 90
  45. ErrObjectDb = 91
  46. ErrObjectRedis = 92
  47. ErrObjectMax = 999
  48. )
  49. // 每个对象100种状态
  50. // 定义10个常用状态
  51. const (
  52. ErrStatusInvalid = 91 // 无效
  53. ErrStatusNotFound = 92 // 未发现
  54. ErrStatusNotEnough = 93 // 不够
  55. ErrStatusError = 94 // 错误
  56. )
  57. const (
  58. Multiple100 = 100
  59. // 全局参数 一般不要用,防止出现问题定位不到在所在程序及位置
  60. // 999 999 001 0000 - 999 999 999 9999
  61. ErrPlayerNoToken = 999_999_000_0000 + errPlayerNoToken*100
  62. ErrPlayerBanLogin = 999_999_000_0000 + errPlayerBanLogin*100
  63. ErrPlayerOtherLogin = 999_999_000_0000 + errPlayerOtherLogin*100
  64. ErrSystemUpgrade = 999_999_000_0000 + ErrObjectSystem*10000 + 1*100
  65. ErrSystemFull = 999_999_000_0000 + ErrObjectSystem*10000 + 2*100
  66. ErrSystemFoundCheat = 999_999_000_0000 + ErrObjectSystem*10000 + 3*100
  67. // 请求参数 100
  68. ErrParamInvalid = ErrObjectParam*Multiple100 + ErrStatusInvalid
  69. // 玩家 200
  70. errPlayerBanLogin = ErrObjectPlayer*Multiple100 + 1
  71. errPlayerOtherLogin = ErrObjectPlayer*Multiple100 + 2
  72. ErrPlayerNotFound = ErrObjectPlayer*Multiple100 + ErrStatusNotFound
  73. errPlayerNoToken = ErrObjectPlayer*Multiple100 + 3
  74. // 道具 300
  75. ErrItemNotFound = ErrObjectItem*Multiple100 + ErrStatusNotFound
  76. ErrItemNotEnough = ErrObjectItem*Multiple100 + ErrStatusNotEnough
  77. // 目标
  78. ErrTargetNotFound = ErrObjectTarget*Multiple100 + ErrStatusNotFound
  79. // 配置
  80. ErrConfigNotFound = ErrObjectConfig*Multiple100 + ErrStatusNotFound
  81. // 条件
  82. ErrConditionNotEnough = ErrObjectCondition*Multiple100 + ErrStatusNotEnough
  83. // 结果
  84. ErrResultError = ErrObjectResult*Multiple100 + ErrStatusError
  85. // 英雄 400
  86. ErrHeroNotFound = ErrObjectHero*Multiple100 + ErrStatusNotFound
  87. // 宝物 500
  88. ErrTreasuresNotFound = ErrObjectTreasures*Multiple100 + ErrStatusNotFound
  89. // 数据库 600
  90. ErrDBExec = ErrObjectDb*Multiple100 + 1
  91. ErrDBLoadNil = ErrObjectDb*Multiple100 + 2
  92. // redis 700
  93. ErrRedisExec = ErrObjectRedis*Multiple100 + 1
  94. // 章节
  95. ErrChpaterNotFound = ErrObjectChapter*Multiple100 + ErrStatusNotFound
  96. //奖励
  97. ErrRewardNotFound = ErrObjectReward*Multiple100 + ErrStatusNotFound
  98. // 配置
  99. // ErrConfigMaxOnline = ErrObjectConfig*Multiple100 + 1
  100. ErrMaxTypeMaxStatus = ErrObjectMax*100 + 99 // 99900+99 = 999_99
  101. ErrMaxVal = 999*1000_000_0000 + 999*1000_0000 + 999*10000 + 99*100 + 99
  102. )
  103. // 每种状态在同一个函数种可以使用100次,需要序号递增
  104. // initErrorCodes 是一个包级初始化函数,用于设置程序编号和模块编号
  105. // login 1:
  106. // world 2: db:1; game:2; gate:3; systemdata:4
  107. // gate 3:
  108. func InitErrorCodes(programID, moduleID int) *ErrorCode {
  109. errorCodes := new(ErrorCode)
  110. errorCodes.ProgramID = programID
  111. errorCodes.ModuleID = moduleID
  112. return errorCodes
  113. }
  114. // GenerateErrorCode 生成错误编号,只需要提供顺序号
  115. func (ec *ErrorCode) GenerateErrorCode(errObj int, errStatus int, seq int) int {
  116. return ec.ProgramID*1000_000_0000 + ec.ModuleID*1000_0000 + errObj*10000 + errStatus*100 + seq
  117. }
  118. // 支持输入格式
  119. // ojbstatus
  120. // objstatus,seq
  121. //obj,status,seq
  122. // promodelobjstatusseq
  123. // promodelobjstatusseq,seq
  124. func (ec *ErrorCode) CalcErrorCode(objStatusSeq ...int) int {
  125. num := len(objStatusSeq)
  126. switch num {
  127. case 0:
  128. return 0
  129. case 1:
  130. // objstatus
  131. // promodelobjstatusseq
  132. if objStatusSeq[0] > ErrObjectMax*10000 {
  133. return objStatusSeq[0]
  134. }
  135. return ec.GenerateErrorCode(objStatusSeq[0]/100, (objStatusSeq[0] % 100), 0)
  136. case 2:
  137. // objstatus,seq
  138. // promodelobjstatusseq,seq
  139. if objStatusSeq[0] > ErrObjectMax*10000 {
  140. return objStatusSeq[0] + objStatusSeq[1]
  141. }
  142. return ec.GenerateErrorCode(objStatusSeq[0]/100, (objStatusSeq[0] % 100), objStatusSeq[1])
  143. case 3:
  144. return ec.GenerateErrorCode(objStatusSeq[0], objStatusSeq[1], objStatusSeq[2])
  145. default:
  146. return 0
  147. }
  148. }
  149. func (ec *ErrorCode) PrintError(errCode int, errMsg string) {
  150. errCode2 := ec.CalcErrorCode(errCode)
  151. // code := (errCode2 % 1000_0000) / 100
  152. // fmt.Printf("%v-%v: %v %s\n", errCode2, errCode2+99, code, errMsg)
  153. fmt.Printf("%v: %s\n", errCode2, errMsg)
  154. }
  155. func PrintErrorCode() {
  156. errorEncoder1 := InitErrorCodes(ServerTypeGate, GateModleGame)
  157. errorEncoder1.PrintError(ErrParamInvalid, "参数错误")
  158. errorEncoder1.PrintError(ErrPlayerBanLogin, "禁止登录")
  159. errorEncoder1.PrintError(ErrPlayerOtherLogin, "异地登录")
  160. errorEncoder1.PrintError(ErrPlayerNotFound, "玩家未登录")
  161. errorEncoder1.PrintError(ErrPlayerNoToken, "玩家凭证不存在")
  162. errorEncoder2 := InitErrorCodes(ServerTypeWorld, WorldModleGame)
  163. errorEncoder2.PrintError(ErrItemNotEnough, "道具不足")
  164. errorEncoder2.PrintError(ErrItemNotFound, "道具不存在")
  165. errorEncoder2.PrintError(ErrHeroNotFound, "英雄不存在")
  166. errorEncoder2.PrintError(ErrTreasuresNotFound, "宝物不存在")
  167. errorEncoder2.PrintError(ErrDBExec, "数据库错误")
  168. errorEncoder2.PrintError(ErrDBLoadNil, "数据库加载错误")
  169. errorEncoder2.PrintError(ErrRedisExec, "缓存错误")
  170. errorEncoder2.PrintError(ErrTargetNotFound, "目标未发现")
  171. errorEncoder2.PrintError(ErrConfigNotFound, "配置未发现")
  172. errorEncoder2.PrintError(ErrConditionNotEnough, "条件不满足")
  173. errorEncoder1.PrintError(ErrPlayerNoToken, "玩家凭证不存在")
  174. errorEncoder1.PrintError(ErrPlayerBanLogin, "禁止登录")
  175. errorEncoder1.PrintError(ErrPlayerOtherLogin, "异地登录")
  176. errorEncoder1.PrintError(ErrSystemUpgrade, "系统维护")
  177. errorEncoder1.PrintError(ErrSystemFull, "服务器满")
  178. errorEncoder1.PrintError(ErrSystemFoundCheat, "数据异常")
  179. // 20010019100:参数错误
  180. // 20010020100:禁止登录
  181. // 20010020200:异地登录
  182. // 20010029200:玩家未登录
  183. // 20010020300:玩家凭证不存在
  184. // 20010020400:达到最大在线数
  185. // 30020030100:道具不足
  186. // 30020039200:道具不存在
  187. // 30020049200:英雄不存在
  188. // 30020059200:宝物不存在
  189. // 30020060100:数据库错误
  190. // 30020060200:数据库加载错误
  191. // 30020070100:缓存错误
  192. }