You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

3 years ago
  1. package middleware
  2. import (
  3. "net"
  4. "net/http"
  5. "net/http/httputil"
  6. "os"
  7. "runtime/debug"
  8. "strings"
  9. "github.com/flipped-aurora/gin-vue-admin/global"
  10. "github.com/gin-gonic/gin"
  11. "go.uber.org/zap"
  12. )
  13. // GinRecovery recover掉项目可能出现的panic,并使用zap记录相关日志
  14. func GinRecovery(stack bool) gin.HandlerFunc {
  15. return func(c *gin.Context) {
  16. defer func() {
  17. if err := recover(); err != nil {
  18. // Check for a broken connection, as it is not really a
  19. // condition that warrants a panic stack trace.
  20. var brokenPipe bool
  21. if ne, ok := err.(*net.OpError); ok {
  22. if se, ok := ne.Err.(*os.SyscallError); ok {
  23. if strings.Contains(strings.ToLower(se.Error()), "broken pipe") || strings.Contains(strings.ToLower(se.Error()), "connection reset by peer") {
  24. brokenPipe = true
  25. }
  26. }
  27. }
  28. httpRequest, _ := httputil.DumpRequest(c.Request, false)
  29. if brokenPipe {
  30. global.GVA_LOG.Error(c.Request.URL.Path,
  31. zap.Any("error", err),
  32. zap.String("request", string(httpRequest)),
  33. )
  34. // If the connection is dead, we can't write a status to it.
  35. _ = c.Error(err.(error)) // nolint: errcheck
  36. c.Abort()
  37. return
  38. }
  39. if stack {
  40. global.GVA_LOG.Error("[Recovery from panic]",
  41. zap.Any("error", err),
  42. zap.String("request", string(httpRequest)),
  43. zap.String("stack", string(debug.Stack())),
  44. )
  45. } else {
  46. global.GVA_LOG.Error("[Recovery from panic]",
  47. zap.Any("error", err),
  48. zap.String("request", string(httpRequest)),
  49. )
  50. }
  51. c.AbortWithStatus(http.StatusInternalServerError)
  52. }
  53. }()
  54. c.Next()
  55. }
  56. }