recover.go 680 B

12345678910111213141516171819202122232425262728
  1. package middleware
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "net/http"
  6. "one-api/common"
  7. "runtime/debug"
  8. )
  9. func RelayPanicRecover() gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. defer func() {
  12. if err := recover(); err != nil {
  13. common.SysError(fmt.Sprintf("panic detected: %v", err))
  14. common.SysError(fmt.Sprintf("stacktrace from panic: %s", string(debug.Stack())))
  15. c.JSON(http.StatusInternalServerError, gin.H{
  16. "error": gin.H{
  17. "message": fmt.Sprintf("Panic detected, error: %v. Please submit a issue here: https://github.com/Calcium-Ion/new-api", err),
  18. "type": "new_api_panic",
  19. },
  20. })
  21. c.Abort()
  22. }
  23. }()
  24. c.Next()
  25. }
  26. }