response.go 2.6 KB

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