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

@@ -95,11 +95,11 @@ func (s *SysPostController) Add(c *gin.Context) {
var body model.SysPost
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.PostId > 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: postId not is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: postId not is empty"))
return
}
@@ -138,11 +138,11 @@ func (s *SysPostController) Edit(c *gin.Context) {
var body model.SysPost
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.PostId <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: postId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: postId is empty"))
return
}
@@ -206,7 +206,7 @@ func (s SysPostController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
postId := c.Param("postId")
if postId == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: postId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: postId is empty"))
return
}
@@ -238,7 +238,7 @@ func (s *SysPostController) Export(c *gin.Context) {
rows, total := s.sysPostService.FindByPage(query)
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
}