Refactor error handling in system and trace controllers

- Updated error response codes for various validation errors from 400 to 422 to better reflect the nature of the errors.
- Changed error messages for empty parameters (e.g., userId, menuId, roleId) to use a consistent error code format.
- Improved error handling in the IPerf, Ping, and WS controllers to provide more informative error messages.
- Ensured that all controllers return appropriate error messages when binding JSON or query parameters fails.
This commit is contained in:
TsMask
2025-04-27 16:38:19 +08:00
parent 56991a0b49
commit 80d612c56c
67 changed files with 424 additions and 410 deletions

View File

@@ -46,7 +46,7 @@ func (s *PacketController) Start(c *gin.Context) {
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -68,7 +68,7 @@ func (s *PacketController) Stop(c *gin.Context) {
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -90,7 +90,7 @@ func (s *PacketController) Filter(c *gin.Context) {
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -112,7 +112,7 @@ func (s *PacketController) KeepAlive(c *gin.Context) {
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -137,7 +137,7 @@ func (s *PacketController) FilePull(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
fileName := fmt.Sprintf("%s.pcap", querys.TaskNo)

View File

@@ -43,10 +43,9 @@ func (s *TCPdumpController) DumpStart(c *gin.Context) {
NeId string `json:"neId" binding:"required"` // 网元ID
Cmd string `json:"cmd" binding:"required"` // 命令 "-n -s 0 -v"
}
err := c.ShouldBindBodyWithJSON(&body)
if err != nil {
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -78,10 +77,9 @@ func (s *TCPdumpController) DumpStop(c *gin.Context) {
NeId string `json:"neId" binding:"required"` // 网元ID
TaskCode string `json:"taskCode" binding:"required"` // 任务码,停止任务并查看日志信息
}
err := c.ShouldBindBodyWithJSON(&body)
if err != nil {
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -113,10 +111,9 @@ func (s *TCPdumpController) UPFTrace(c *gin.Context) {
NeId string `json:"neId" binding:"required"` // 网元ID
Cmd string `json:"cmd" binding:"required"` // 命令
}
err := c.ShouldBindBodyWithJSON(&body)
if err != nil {
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}

View File

@@ -37,7 +37,7 @@ func (s *TraceDataController) List(c *gin.Context) {
func (s *TraceDataController) Info(c *gin.Context) {
id := parse.Number(c.Param("id"))
if id <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty"))
return
}
@@ -56,7 +56,7 @@ func (s *TraceDataController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Param("id")
if id == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty"))
return
}

View File

@@ -43,7 +43,7 @@ func (s *TraceTaskController) List(c *gin.Context) {
func (s *TraceTaskController) Info(c *gin.Context) {
id := parse.Number(c.Param("id"))
if id <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty"))
return
}
@@ -61,19 +61,18 @@ func (s *TraceTaskController) Info(c *gin.Context) {
func (s *TraceTaskController) Add(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body model.TraceTask
err := c.ShouldBindBodyWithJSON(&body)
if err != nil {
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
if body.ID > 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: id not is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id not is empty"))
return
}
body.CreateBy = reqctx.LoginUserToUserName(c)
if err = s.traceTaskService.Insert(body); err != nil {
if err := s.traceTaskService.Insert(body); err != nil {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
return
}
@@ -87,7 +86,7 @@ func (s *TraceTaskController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Param("id")
if id == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty"))
return
}
@@ -112,12 +111,12 @@ func (s *TraceTaskController) Remove(c *gin.Context) {
//
// GET /filePull
func (s *TraceTaskController) FilePull(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var querys struct {
TraceId string `form:"traceId" binding:"required"`
}
if err := c.ShouldBindQuery(&querys); err != nil {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}

View File

@@ -36,10 +36,10 @@ type TraceTaskHlrController struct {
//
// GET /list
func (s *TraceTaskHlrController) List(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var query model.TraceTaskHlrQuery
if err := c.ShouldBindQuery(&query); err != nil {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -54,7 +54,7 @@ func (s *TraceTaskHlrController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Param("id")
if id == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty"))
return
}
@@ -79,7 +79,6 @@ func (s *TraceTaskHlrController) Remove(c *gin.Context) {
//
// POST /start
func (s *TraceTaskHlrController) Start(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body struct {
IMSI string `json:"imsi"` // IMSI
MSISDN string `json:"msisdn"` // MSISDN
@@ -88,12 +87,13 @@ func (s *TraceTaskHlrController) Start(c *gin.Context) {
Remark string `json:"remark"` // 备注说明
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
if body.IMSI == "" && body.MSISDN == "" {
c.JSON(400, resp.CodeMsg(400, "imsi amd msisdn is empty"))
c.JSON(422, resp.CodeMsg(422002, "imsi amd msisdn is empty"))
return
}
@@ -117,12 +117,12 @@ func (s *TraceTaskHlrController) Start(c *gin.Context) {
//
// POST /stop
func (s *TraceTaskHlrController) Stop(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body struct {
ID string `json:"id" binding:"required"` // 任务ID
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -162,13 +162,13 @@ func (s *TraceTaskHlrController) File(c *gin.Context) {
}
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
task := s.traceTaskHlrService.FindById(body.ID)
if task.ID == 0 || task.ID != body.ID {
c.JSON(200, resp.CodeMsg(400, "task not found"))
c.JSON(422, resp.CodeMsg(422002, "task not found"))
return
}
@@ -194,14 +194,14 @@ func (s *TraceTaskHlrController) FilePull(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(40422, errMsgs))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
// 查询网元获取IP
neInfo := s.neInfoService.FindByNeTypeAndNeID(querys.NeType, querys.NeID)
if neInfo.NeId != querys.NeID || neInfo.IP == "" {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
c.JSON(422, resp.CodeMsg(422002, i18n.TKey(language, "app.common.noNEInfo")))
return
}