155 lines
3.3 KiB
Go
Executable File
155 lines
3.3 KiB
Go
Executable File
package exception
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"video/utils/code"
|
|
)
|
|
|
|
type E struct {
|
|
Code int `json:"code"`
|
|
Msg any `json:"msg"`
|
|
Err error `json:"-"`
|
|
}
|
|
|
|
func (r E) Error() string {
|
|
if r.Err != nil {
|
|
return fmt.Sprintf(`{\"code\":%d;\"msg\":\"%v\",\"err\":\"%s\"}`, r.Code, r.Msg, r.Err.Error())
|
|
} else {
|
|
return fmt.Sprintf(`{\"code\":%d;\"msg\":\"%v\"}`, r.Code, r.Msg)
|
|
}
|
|
}
|
|
|
|
func (r E) Return(c *gin.Context) {
|
|
c.JSON(http.StatusOK, r)
|
|
}
|
|
|
|
func NE(msg any) error {
|
|
return NEC(msg, code.ERROR)
|
|
}
|
|
|
|
func NEC(msg any, code int) error {
|
|
return &E{
|
|
Msg: msg,
|
|
Code: code,
|
|
}
|
|
}
|
|
|
|
func NEE(err error, msg string) error {
|
|
return NEEC(err, msg, code.ERROR)
|
|
}
|
|
|
|
func NEEC(err error, msg string, code int) error {
|
|
if err != nil {
|
|
return &E{
|
|
Msg: msg,
|
|
Code: code,
|
|
Err: err,
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func PM(msg any) {
|
|
panic(&E{code.ERROR, msg, nil})
|
|
}
|
|
func PMC(msg any, code int) {
|
|
panic(&E{code, msg, nil})
|
|
}
|
|
|
|
func PBM(flag bool, msg any) {
|
|
if flag {
|
|
panic(&E{code.ERROR, msg, nil})
|
|
}
|
|
}
|
|
func PBMC(flag bool, msg any, code int) {
|
|
if flag {
|
|
panic(&E{code, msg, nil})
|
|
}
|
|
}
|
|
|
|
func PEM(err error, msg any) {
|
|
if err != nil {
|
|
panic(&E{code.ERROR, msg, err})
|
|
}
|
|
}
|
|
func PEMC(err error, msg any, code int) {
|
|
if err != nil {
|
|
panic(&E{code, msg, err})
|
|
}
|
|
}
|
|
|
|
//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})
|
|
// }
|
|
//}
|