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

@@ -82,7 +82,7 @@ func (s *SysMenuController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
menuId := parse.Number(c.Param("menuId"))
if menuId <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: menuId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: menuId is empty"))
return
}
@@ -105,11 +105,11 @@ func (s *SysMenuController) Add(c *gin.Context) {
var body model.SysMenu
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.MenuId > 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: menuId not is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: menuId not is empty"))
return
}
@@ -158,11 +158,11 @@ func (s *SysMenuController) Edit(c *gin.Context) {
var body model.SysMenu
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.MenuId <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: menuId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: menuId is empty"))
return
}
@@ -278,7 +278,7 @@ func (s SysMenuController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
menuId := parse.Number(c.Param("menuId"))
if menuId <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: menuId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: menuId is empty"))
return
}
@@ -359,7 +359,7 @@ func (s *SysMenuController) TreeRole(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
roleId := parse.Number(c.Param("roleId"))
if roleId <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: roleId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: roleId is empty"))
return
}