response.go 792 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. @Time : 2019-11-07 14:55
  3. @Author : zr
  4. */
  5. package middleware
  6. import (
  7. "github.com/gin-gonic/gin"
  8. "net/http"
  9. "video_course/errors"
  10. "video_course/global"
  11. )
  12. type ResponseBase struct {
  13. Code int
  14. Memo string
  15. }
  16. func ErrorResponse() gin.HandlerFunc {
  17. return func(c *gin.Context) {
  18. defer func() {
  19. if len(c.Errors) > 0 {
  20. stdErr := errors.FromError(c.Errors[0].Err)
  21. var resp interface{}
  22. resp1 := &ResponseBase{
  23. Code: int(stdErr.Code),
  24. Memo: errors.ErrorCode(stdErr.Code).ShowMsg(),
  25. }
  26. resp = resp1
  27. if global.Project.Debug {
  28. var resp2 struct {
  29. ResponseBase
  30. DebugMsg string
  31. }
  32. resp2.ResponseBase = *resp1
  33. resp2.DebugMsg = stdErr.Error()
  34. resp = resp2
  35. }
  36. c.JSON(http.StatusOK, resp)
  37. }
  38. }()
  39. c.Next()
  40. }
  41. }