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

@@ -81,7 +81,7 @@ func (s *SysUserController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
userId := parse.Number(c.Param("userId"))
if userId < 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: userId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: userId is empty"))
return
}
@@ -164,7 +164,8 @@ func (s *SysUserController) Add(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body model.SysUser
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
c.JSON(400, resp.ErrMsg(resp.FormatBindError(err)))
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
@@ -174,13 +175,13 @@ func (s *SysUserController) Add(c *gin.Context) {
}
if err := c.ShouldBindBodyWithJSON(&bodyPassword); 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
}
body.Password = bodyPassword.Password
if body.UserId > 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: userId not is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: userId not is empty"))
return
}
if !regular.ValidUsername(body.UserName) {
@@ -278,11 +279,11 @@ func (s *SysUserController) Edit(c *gin.Context) {
var body model.SysUser
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.UserId <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: userId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: userId is empty"))
return
}
@@ -378,7 +379,7 @@ func (s *SysUserController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
userId := c.Param("userId")
if userId == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: userId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: userId is empty"))
return
}
@@ -426,7 +427,7 @@ func (s *SysUserController) Password(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
}
@@ -477,7 +478,7 @@ func (s *SysUserController) Status(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
}
@@ -525,7 +526,7 @@ func (s *SysUserController) Export(c *gin.Context) {
rows, total := s.sysUserService.FindByPage(queryMap, dataScopeSQL)
if total == 0 {
// c.JSON(200, resp.CodeMsg(40016, "export data record as empty"))
c.JSON(200, resp.CodeMsg(40016, i18n.TKey(language, "app.common.exportEmpty")))
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
return
}
@@ -647,7 +648,7 @@ func (s *SysUserController) 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
}