response.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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"] = "success"
  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. })
  79. } else {
  80. c.JSON(http.StatusOK, gin.H{
  81. "data": "",
  82. "code": errorCode,
  83. "msg": msg,
  84. })
  85. }
  86. c.Abort()
  87. }
  88. func ValidateFail(c *gin.Context, msg string) {
  89. Fail(c, global.Errors.ValidateError.ErrorCode, msg)
  90. }
  91. // 422 参数错误使用
  92. func ParameterError(c *gin.Context, msg interface{}) {
  93. finalMsg := "参数错误"
  94. if err, ok := msg.(error); ok {
  95. // release 版本屏蔽掉报错
  96. if !config.IsProduction() {
  97. finalMsg = err.Error()
  98. }
  99. } else if str, ok := msg.(string); ok {
  100. finalMsg = str
  101. }
  102. c.JSON(http.StatusUnprocessableEntity, gin.H{
  103. "code": http.StatusUnprocessableEntity,
  104. "msg": finalMsg,
  105. })
  106. c.Abort()
  107. }
  108. // 401 权限错误
  109. func UnauthorizedRequestsFail(c *gin.Context, msg string) {
  110. //c.JSON(http.StatusUnauthorized, gin.H{
  111. c.JSON(http.StatusOK, gin.H{
  112. "code": http.StatusUnauthorized,
  113. "msg": msg,
  114. })
  115. c.Abort()
  116. }