exception.go 418 B

123456789101112131415161718192021222324
  1. package exception
  2. import (
  3. "exam_system/result"
  4. "fmt"
  5. "github.com/gin-gonic/gin"
  6. "net/http"
  7. "runtime/debug"
  8. )
  9. func Recover() gin.HandlerFunc {
  10. return func(c *gin.Context) {
  11. defer func() {
  12. if r := recover(); r != nil {
  13. fmt.Errorf("panic %v\n", r)
  14. debug.PrintStack()
  15. c.JSON(http.StatusInternalServerError, result.UNKNOW_ERROR.SetMsg(r.(error).Error()))
  16. c.Abort()
  17. }
  18. }()
  19. c.Next()
  20. }
  21. }