response.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package response
  2. import (
  3. "designs/config"
  4. "designs/global"
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "github.com/gin-gonic/gin"
  10. "github.com/pkg/errors"
  11. "go.uber.org/zap"
  12. )
  13. // 500 程序奔溃
  14. func ServerError(c *gin.Context, msg string) {
  15. errMsg := "服务程序错误"
  16. if !config.IsProduction() {
  17. stack := strings.Split(strings.Replace(msg, "\t", "--", -1), "\n")
  18. c.JSON(http.StatusInternalServerError, gin.H{
  19. "code": http.StatusInternalServerError,
  20. "msg": errMsg,
  21. "stack": stack,
  22. })
  23. } else {
  24. c.JSON(http.StatusInternalServerError, gin.H{
  25. "code": http.StatusInternalServerError,
  26. "msg": errMsg,
  27. })
  28. }
  29. c.Abort()
  30. }
  31. func Success(c *gin.Context, data gin.H) {
  32. //for k, v := range data {
  33. //
  34. //}
  35. data["code"] = 0
  36. //if data["data"] == nil {
  37. // data["data"] = nil
  38. //}
  39. if data["msg"] == nil {
  40. data["msg"] = "success"
  41. }
  42. //fmt.Println(data)
  43. data["timestamp"] = time.Now().Unix()
  44. c.JSON(http.StatusOK, data)
  45. }
  46. type causer interface {
  47. Format(s fmt.State, verb rune)
  48. }
  49. func Next(c *gin.Context, resp interface{}) {
  50. c.JSON(http.StatusOK, resp)
  51. }
  52. func Fail(c *gin.Context, errorCode int, e interface{}) {
  53. var msg string
  54. var stack []string
  55. if _, ok := e.(string); ok {
  56. msg = e.(string)
  57. } else if err, ok := e.(error); ok {
  58. if _, ok := e.(causer); ok {
  59. msg = strings.Split(fmt.Sprintf("%v", errors.WithStack(err)), ":")[0]
  60. if !config.IsProduction() {
  61. tmp := fmt.Sprintf("%+v", errors.WithStack(err))
  62. stack = strings.Split(strings.Replace(tmp, "\t", "--", -1), "\n")
  63. }
  64. } else {
  65. msg = fmt.Sprintf(err.Error())
  66. if !config.IsProduction() {
  67. tmp := fmt.Sprintf("%v", zap.StackSkip("", 1))
  68. stack = strings.Split(strings.Replace(tmp, "\t", "--", -1), "\n")
  69. }
  70. }
  71. }
  72. if len(stack) > 0 {
  73. c.JSON(http.StatusOK, gin.H{
  74. "code": errorCode,
  75. "msg": msg,
  76. "data": "",
  77. "stack": stack,
  78. "timestamp": time.Now().Unix(),
  79. })
  80. } else {
  81. c.JSON(http.StatusOK, gin.H{
  82. "data": "",
  83. "code": errorCode,
  84. "msg": msg,
  85. "timestamp": time.Now().Unix(),
  86. })
  87. }
  88. c.Abort()
  89. }
  90. func ValidateFail(c *gin.Context, msg string) {
  91. Fail(c, global.Errors.ValidateError.ErrorCode, msg)
  92. }
  93. // 422 参数错误使用
  94. func ParameterError(c *gin.Context, msg interface{}) {
  95. finalMsg := "参数错误"
  96. if err, ok := msg.(error); ok {
  97. // release 版本屏蔽掉报错
  98. if !config.IsProduction() {
  99. finalMsg = err.Error()
  100. }
  101. } else if str, ok := msg.(string); ok {
  102. finalMsg = str
  103. }
  104. c.JSON(http.StatusUnprocessableEntity, gin.H{
  105. "code": http.StatusUnprocessableEntity,
  106. "msg": finalMsg,
  107. })
  108. c.Abort()
  109. }
  110. // 401 权限错误
  111. func UnauthorizedRequestsFail(c *gin.Context, msg string) {
  112. //c.JSON(http.StatusUnauthorized, gin.H{
  113. c.JSON(http.StatusOK, gin.H{
  114. "code": http.StatusUnauthorized,
  115. "msg": msg,
  116. })
  117. c.Abort()
  118. }