gerror.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. package gerror
  2. import (
  3. "runtime"
  4. "strings"
  5. "github.com/gin-gonic/gin"
  6. )
  7. type GCODE int
  8. //ANCHOR temporary error code
  9. const (
  10. OK = 100
  11. //health check
  12. HEALTHY = 110
  13. //mysqlError
  14. InvalidConnection = 200
  15. NotFoundRecord = 201
  16. TooManyConnection = 203
  17. MysqlTimeout = 204
  18. MysqlContextError = 205
  19. MysqlSaveError = 206
  20. CouldNotAppendRow = 207
  21. //redis
  22. RedisError = 220
  23. NotMatchedValueError = 221
  24. //go
  25. Error3rdParty = 250
  26. GoContextError = 251
  27. MultipartError = 252
  28. FileUploadError = 253
  29. // Error3rdParty중에서 iamport
  30. IamPortImpUIDNotFound = 270
  31. IamPortUnauthorized = 271
  32. // Error3rdParty중에서 SweetTracker
  33. SweetTrackerNotDelivered = 280
  34. //badRequest
  35. UnsupportedFileFormat = 300
  36. InvalidParameterValue = 301
  37. InvalidParameterType = 302
  38. DuplicateValue = 303
  39. //authorized
  40. Unauthorized = 400
  41. MakeSessionError = 401
  42. GetSNSUserInfoError = 402
  43. LoginFailed = 403
  44. NotViralUser = 404
  45. PermissionNotFound = 405
  46. //brand
  47. NotVerifyError = 500
  48. //order
  49. ProductOptionStockError = 520
  50. TimesaleOptionStockError = 521
  51. IamPortError = 522
  52. UnVerifiedPayment = 523
  53. UndeliverableArea = 524
  54. ShippingPolicyCountOver = 525
  55. NotYet = 526
  56. //feed
  57. AlreadyReported = 550
  58. FeedUnauthorizedUser = 551
  59. //point
  60. PointRegisterError = 570
  61. PointDeletedError = 571
  62. //viral point
  63. OverflowRequestAmount = 580
  64. ViralPointRegisterError = 581
  65. // certification
  66. DuplicateCertificationUID = 600
  67. NotMatchPhone = 601
  68. NoCertificationHistory = 602
  69. // viral_user
  70. AlreadyExisted = 650
  71. //blockchain
  72. BlockChainError = 700
  73. NotHaveUserWallet = 701
  74. NotEnoughLeftBalance = 702
  75. FailToSweep = 703
  76. // http error
  77. HttpRequestWentWrong = 800
  78. //Log error
  79. LogTrackingError = 900
  80. //common
  81. NothingToProcess = 5000 // 처리할 데이터가 없을 경우 사용하는 일반적인 에러
  82. )
  83. var statusText = map[GCODE]string{
  84. OK: "OK",
  85. HEALTHY: "Calm down, I'm helathy",
  86. InvalidConnection: "Invalid connection",
  87. NotFoundRecord: "Could not found matched record",
  88. TooManyConnection: "Too many connection(Count overflow)",
  89. MysqlTimeout: "mysql lock wait timeout",
  90. MysqlContextError: "mysql context error",
  91. MysqlSaveError: "Error while saving",
  92. CouldNotAppendRow: "Cannot append a row ",
  93. RedisError: "Record Error(Redis)",
  94. NotMatchedValueError: "Mismatch in phone between Redis,RDB",
  95. Error3rdParty: "Error occurred 3rd party service",
  96. GoContextError: "Error occurred at go",
  97. MultipartError: "multipart error",
  98. FileUploadError: "Error occurred during file upload",
  99. IamPortImpUIDNotFound: "IamPort data not found from imp_uid",
  100. IamPortUnauthorized: "IamPort accetoken invalid",
  101. SweetTrackerNotDelivered: "SweetTracker response not delivered",
  102. UnsupportedFileFormat: "Unsupported file format",
  103. InvalidParameterValue: "Invalid parameter value",
  104. InvalidParameterType: "Invalid parameter's type",
  105. DuplicateValue: "Duplicate value",
  106. PermissionNotFound: "Permission not found",
  107. Unauthorized: "Unauthorized request",
  108. MakeSessionError: "Error occurred during make session",
  109. GetSNSUserInfoError: "Get SNS user information error",
  110. NotVerifyError: "Not Verify Error",
  111. LoginFailed: "Fail to login",
  112. NotViralUser: "Authorized but, not viral_user",
  113. ProductOptionStockError: "Product option stock error",
  114. TimesaleOptionStockError: "Timesale option stock error",
  115. IamPortError: "Iamport Error",
  116. UnVerifiedPayment: "Unverified payment",
  117. UndeliverableArea: "Undeliverable Area",
  118. ShippingPolicyCountOver: "Over Shipping policy count",
  119. NotYet: "It's not the right time. Can't be completed because of date",
  120. AlreadyReported: "Already Reported",
  121. FeedUnauthorizedUser: "Feed Unauthorized User",
  122. PointRegisterError: "Point Register Error",
  123. PointDeletedError: "Point Delete Error",
  124. OverflowRequestAmount: "The request amount is bigger than I had.",
  125. ViralPointRegisterError: "Viral Point Register Error",
  126. DuplicateCertificationUID: "Already exist certification row in database",
  127. NotMatchPhone: "Not match phone between user_phone, certification_phone",
  128. NoCertificationHistory: "When convert common_user to viral_user, there is no certification_phone history",
  129. AlreadyExisted: "Already exist viral_user",
  130. BlockChainError: "BlockChain Error",
  131. NotHaveUserWallet: "Doesn't have any user wallet",
  132. NotEnoughLeftBalance: "Not enough left balance.",
  133. FailToSweep: "Fail to sweep",
  134. HttpRequestWentWrong: "Http Request Went Wrong Some reasone",
  135. LogTrackingError: "Error in tracking and storing logs.",
  136. NothingToProcess: "There is nothing to process.",
  137. }
  138. func convertGcodeToText(code GCODE) string {
  139. return statusText[code]
  140. }
  141. func traceFunctionName() string {
  142. pc, _, _, _ := runtime.Caller(2)
  143. fulllName := runtime.FuncForPC(pc).Name()
  144. funcName := strings.Split(fulllName, ".")
  145. return funcName[len(funcName)-1]
  146. }
  147. func IntegratedResponseToRequest(c *gin.Context, statusCode int, gcode GCODE, data interface{}, err error) {
  148. if statusCode == 200 {
  149. if data == nil {
  150. c.Status(statusCode)
  151. return
  152. }
  153. c.JSON(statusCode, data)
  154. return
  155. } else {
  156. c.JSON(statusCode, gin.H{
  157. "payload": data,
  158. "gcode": gcode,
  159. "gcode_message": convertGcodeToText(gcode),
  160. "excute_function": traceFunctionName(),
  161. "error": err,
  162. })
  163. return
  164. }
  165. }