Files
2024-08-26 17:20:13 +08:00

44 lines
1.2 KiB
Plaintext

package router
import (
"energy-management-system/utils/exception"
"github.com/gin-gonic/gin"
"net/http"
)
type ExceptionResponse struct {
Code int `json:"code"`
Msg interface{} `json:"msg"`
Path string `json:"path"`
}
type Exception struct {
HttpCode int `json:"-"`
Code int `json:"code"`
Msg interface{} `json:"msg"`
Err error `json:"err"`
}
func Panic(c *gin.Context, e *Exception) {
c.JSON(e.HttpCode, ExceptionResponse{e.Code, e.Msg, GetReqPath(c)})
}
func Unknow(c *gin.Context) {
c.JSON(http.StatusForbidden, ExceptionResponse{exception.UNKNOW_ERROR, "未知错误", GetReqPath(c)})
}
func Server(c *gin.Context) {
c.JSON(http.StatusInternalServerError, ExceptionResponse{exception.SERVER_ERROR, "服务器错误", GetReqPath(c)})
}
func NotFoundR(c *gin.Context) {
c.JSON(http.StatusForbidden, ExceptionResponse{exception.NOT_FOUND_ROUTE, "Not Found Route", GetReqPath(c)})
}
func NotFoundM(c *gin.Context) {
c.JSON(http.StatusForbidden, ExceptionResponse{exception.NOT_FOUND_METH, "Not Found Method", GetReqPath(c)})
}
// GetReqPath 获取请求路径
func GetReqPath(c *gin.Context) string {
return c.Request.Method + " " + c.Request.URL.String()
}