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

@@ -49,10 +49,9 @@ type UDMAuthController struct {
// @Description UDM Authenticated User Data List Refresh Synchronization Latest
// @Router /neData/udm/auth/resetData/{neId} [put]
func (s *UDMAuthController) 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 *UDMAuthController) 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/imsi is empty"))
return
}
@@ -156,14 +155,18 @@ func (s *UDMAuthController) 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.UDMAuthUser
err := c.ShouldBindBodyWithJSON(&body)
if err != nil || body.IMSI == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
if body.IMSI == "" {
c.JSON(422, resp.CodeMsg(422002, "bind err: imsi is empty"))
return
}
@@ -217,14 +220,18 @@ func (s *UDMAuthController) 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.UDMAuthUser
err := c.ShouldBindBodyWithJSON(&body)
if err != nil || body.IMSI == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
if body.IMSI == "" {
c.JSON(422, resp.CodeMsg(422002, "bind err: imsi is empty"))
return
}
@@ -276,14 +283,18 @@ func (s *UDMAuthController) 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.UDMAuthUser
err := c.ShouldBindBodyWithJSON(&body)
if err != nil || body.IMSI == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
if body.IMSI == "" {
c.JSON(422, resp.CodeMsg(422002, "bind err: imsi is empty"))
return
}
@@ -336,7 +347,7 @@ func (s *UDMAuthController) Remove(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/imsi is empty"))
return
}
@@ -402,7 +413,7 @@ func (s *UDMAuthController) Removes(c *gin.Context) {
imsi := c.Param("imsi")
num := c.Param("num")
if neId == "" || imsi == "" || 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
}
@@ -458,7 +469,7 @@ func (s *UDMAuthController) Export(c *gin.Context) {
neId := c.Query("neId")
fileType := c.Query("type")
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
}
if !(fileType == "csv" || fileType == "txt") {
@@ -546,7 +557,7 @@ func (s *UDMAuthController) 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
}