middleware.go 582 B

12345678910111213141516171819202122232425262728
  1. /*
  2. * @desc:公用中间件
  3. * @company:云南奇讯科技有限公司
  4. * @Author: yixiaohu
  5. * @Date: 2022/3/2 15:16
  6. */
  7. package libMiddleware
  8. import (
  9. "github.com/gogf/gf/v2/frame/g"
  10. "github.com/gogf/gf/v2/net/ghttp"
  11. "github.com/gogf/gf/v2/text/gstr"
  12. )
  13. // ExceptionHandle 异常处理
  14. func ExceptionHandle(r *ghttp.Request) {
  15. r.Middleware.Next()
  16. if err := r.GetError(); err != nil {
  17. msg := err.Error()
  18. pos := gstr.Pos(msg, ":")
  19. if pos > 0 {
  20. msg = gstr.SubStr(msg, pos+2)
  21. }
  22. r.Response.ClearBuffer()
  23. r.Response.WriteJson(g.Map{"code": 500, "message": msg})
  24. }
  25. }