82 lines
2.0 KiB
Go
82 lines
2.0 KiB
Go
package exception
|
|
|
|
import (
|
|
"energy-management-system/utils"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Exception struct {
|
|
HttpCode int `json:"-"`
|
|
Code int `json:"code"`
|
|
Msg interface{} `json:"msg"`
|
|
Err error `json:"err"`
|
|
}
|
|
|
|
type ExceptionResponse struct {
|
|
Code int `json:"code"`
|
|
Msg interface{} `json:"msg"`
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
func (r *Exception) Error() interface{} {
|
|
return r.Msg
|
|
}
|
|
|
|
func NotFoundR(c *gin.Context) {
|
|
c.JSON(http.StatusForbidden, ExceptionResponse{NOT_FOUND_ROUTE, "Not Found Route", utils.GetReqPath(c)})
|
|
}
|
|
|
|
func NotFoundM(c *gin.Context) {
|
|
c.JSON(http.StatusForbidden, ExceptionResponse{NOT_FOUND_METH, "Not Found Method", utils.GetReqPath(c)})
|
|
}
|
|
|
|
func Panic(c *gin.Context, e *Exception) {
|
|
c.JSON(e.HttpCode, ExceptionResponse{e.Code, e.Msg, utils.GetReqPath(c)})
|
|
}
|
|
func Unknow(c *gin.Context) {
|
|
c.JSON(http.StatusForbidden, ExceptionResponse{UNKNOW_ERROR, "未知错误", utils.GetReqPath(c)})
|
|
}
|
|
func Server(c *gin.Context) {
|
|
c.JSON(http.StatusInternalServerError, ExceptionResponse{SERVER_ERROR, "服务器错误", utils.GetReqPath(c)})
|
|
}
|
|
|
|
// FailMsg 主动抛出错误Exception类型
|
|
func FailMsg(msg interface{}) *Exception {
|
|
return &Exception{http.StatusOK, ERROR, msg, nil}
|
|
}
|
|
|
|
func FailCodeMsg(code int, msg string) *Exception {
|
|
return &Exception{http.StatusOK, code, msg, nil}
|
|
}
|
|
|
|
func PanicMsg(msg interface{}) {
|
|
PanicMsgBool(true, msg)
|
|
}
|
|
func PanicCodeMsg(code int, msg string) {
|
|
PanicCodeMsgBool(true, code, msg)
|
|
}
|
|
|
|
func PanicMsgBool(flag bool, msg interface{}) {
|
|
if flag {
|
|
panic(&Exception{http.StatusOK, ERROR, msg, nil})
|
|
}
|
|
}
|
|
func PanicCodeMsgBool(flag bool, code int, msg string) {
|
|
if flag {
|
|
panic(&Exception{http.StatusOK, code, msg, nil})
|
|
}
|
|
}
|
|
|
|
func PanicMsgErr(err error, msg interface{}) {
|
|
if err != nil {
|
|
panic(&Exception{http.StatusOK, ERROR, msg, err})
|
|
}
|
|
}
|
|
func PanicCodeMsgErr(err error, code int, msg string) {
|
|
if err != nil {
|
|
panic(&Exception{http.StatusOK, code, msg, err})
|
|
}
|
|
}
|