package catch

import (
	"fmt"
	"git.168cad.top/zhengqiuyun/rym-util/exception"
	"git.168cad.top/zhengqiuyun/rym-util/getWay"
	"github.com/gin-gonic/gin"
	"runtime"
	"strings"
)

func ExceptionCatch(c *gin.Context) {
	err := recover() //获取异常
	if err != nil {
		switch err.(type) {
		case exception.DError:
			e := (err).(exception.DError)
			getWay.FailAndMsg(c, e.Error())
			break
		case error:
			e := (err).(error)
			if strings.Index(e.Error(), "duplicate key") > 0 {
				getWay.FailAndMsg(c, "信息重复")
			} else {
				var buf [1024]byte
				n := runtime.Stack(buf[:], true)
				fmt.Println(string(buf[:]), n)
				getWay.FailAndMsg(c, e.Error())
			}
			break
		}
	}
}