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

@@ -48,10 +48,9 @@ type UDMSubController struct {
// @Description UDM Subscriber User Reload Data
// @Router /neData/udm/sub/resetData/{neId} [put]
func (s *UDMSubController) ResetData(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
neId := c.Param("neId")
if neId == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neId is empty"))
return
}
@@ -101,7 +100,7 @@ func (s *UDMSubController) Info(c *gin.Context) {
neId := c.Param("neId")
imsi := c.Param("imsi")
if neId == "" || imsi == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neId or imsi is empty"))
return
}
@@ -156,18 +155,18 @@ func (s *UDMSubController) Add(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
neId := c.Param("neId")
if neId == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neId is empty"))
return
}
var body model.UDMSubUser
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 len(body.IMSI) != 15 {
c.JSON(400, resp.CodeMsg(40010, "bind err: IMSI length is not 15 bits"))
c.JSON(422, resp.CodeMsg(422002, "bind err: IMSI length is not 15 bits"))
return
}
@@ -222,18 +221,18 @@ func (s *UDMSubController) Adds(c *gin.Context) {
neId := c.Param("neId")
num := c.Param("num")
if neId == "" || num == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neId/num is empty"))
return
}
var body model.UDMSubUser
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 len(body.IMSI) != 15 {
c.JSON(400, resp.CodeMsg(40010, "bind err: IMSI length is not 15 bits"))
c.JSON(422, resp.CodeMsg(422002, "bind err: IMSI length is not 15 bits"))
return
}
@@ -288,18 +287,18 @@ func (s *UDMSubController) Edit(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
neId := c.Param("neId")
if neId == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neId is empty"))
return
}
var body model.UDMSubUser
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 len(body.IMSI) != 15 {
c.JSON(400, resp.CodeMsg(40010, "bind err: IMSI length is not 15 bits"))
c.JSON(422, resp.CodeMsg(422002, "bind err: IMSI length is not 15 bits"))
return
}
@@ -353,7 +352,7 @@ func (s *UDMSubController) Remove(c *gin.Context) {
neId := c.Param("neId")
imsi := c.Param("imsi")
if neId == "" || len(imsi) < 15 {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neId/imsi is empty"))
return
}
@@ -419,7 +418,7 @@ func (s *UDMSubController) Removes(c *gin.Context) {
imsi := c.Param("imsi")
num := c.Param("num")
if neId == "" || len(imsi) < 15 || num == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neId/imsi/num is empty"))
return
}
@@ -476,7 +475,7 @@ func (s *UDMSubController) Export(c *gin.Context) {
neId := c.Query("neId")
fileType := c.Query("type")
if neId == "" || fileType == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neId or type is empty"))
return
}
if !(fileType == "csv" || fileType == "txt") {
@@ -556,7 +555,7 @@ func (s *UDMSubController) Import(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
}