response.go 2.5 KB

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