This commit is contained in:
2024-08-26 17:20:13 +08:00
commit 51090658a2
39 changed files with 2231 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
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})
}
}