response.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. data["msg"] = ""
  36. //fmt.Println(data)
  37. c.JSON(http.StatusOK, data)
  38. }
  39. type causer interface {
  40. Format(s fmt.State, verb rune)
  41. }
  42. func Next(c *gin.Context, resp interface{}) {
  43. c.JSON(http.StatusOK, resp)
  44. }
  45. func Fail(c *gin.Context, errorCode int, e interface{}) {
  46. var msg string
  47. var stack []string
  48. if _, ok := e.(string); ok {
  49. msg = e.(string)
  50. } else if err, ok := e.(error); ok {
  51. if _, ok := e.(causer); ok {
  52. msg = strings.Split(fmt.Sprintf("%v", errors.WithStack(err)), ":")[0]
  53. if !config.IsProduction() {
  54. tmp := fmt.Sprintf("%+v", errors.WithStack(err))
  55. stack = strings.Split(strings.Replace(tmp, "\t", "--", -1), "\n")
  56. }
  57. } else {
  58. msg = fmt.Sprintf(err.Error())
  59. if !config.IsProduction() {
  60. tmp := fmt.Sprintf("%v", zap.StackSkip("", 1))
  61. stack = strings.Split(strings.Replace(tmp, "\t", "--", -1), "\n")
  62. }
  63. }
  64. }
  65. if len(stack) > 0 {
  66. c.JSON(http.StatusOK, gin.H{
  67. "code": errorCode,
  68. "msg": msg,
  69. "stack": stack,
  70. })
  71. } else {
  72. c.JSON(http.StatusOK, gin.H{
  73. "code": errorCode,
  74. "msg": msg,
  75. })
  76. }
  77. c.Abort()
  78. }
  79. func ValidateFail(c *gin.Context, msg string) {
  80. Fail(c, global.Errors.ValidateError.ErrorCode, msg)
  81. }
  82. // 422 参数错误使用
  83. func ParameterError(c *gin.Context, msg interface{}) {
  84. finalMsg := "参数错误"
  85. if err, ok := msg.(error); ok {
  86. // release 版本屏蔽掉报错
  87. if !config.IsProduction() {
  88. finalMsg = err.Error()
  89. }
  90. } else if str, ok := msg.(string); ok {
  91. finalMsg = str
  92. }
  93. c.JSON(http.StatusUnprocessableEntity, gin.H{
  94. "code": http.StatusUnprocessableEntity,
  95. "msg": finalMsg,
  96. })
  97. c.Abort()
  98. }
  99. // 401 权限错误
  100. func UnauthorizedRequestsFail(c *gin.Context, msg string) {
  101. //c.JSON(http.StatusUnauthorized, gin.H{
  102. c.JSON(http.StatusOK, gin.H{
  103. "code": http.StatusUnauthorized,
  104. "msg": msg,
  105. })
  106. c.Abort()
  107. }