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

@@ -74,7 +74,7 @@ func (s *SysConfigController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
configId := parse.Number(c.Param("configId"))
if configId <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: configId is empty"))
return
}
@@ -98,11 +98,11 @@ func (s *SysConfigController) Add(c *gin.Context) {
var body model.SysConfig
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.ConfigId > 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: configId not is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: configId not is empty"))
return
}
@@ -132,11 +132,11 @@ func (s *SysConfigController) Edit(c *gin.Context) {
var body model.SysConfig
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.ConfigId <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: configId is empty"))
return
}
@@ -197,7 +197,7 @@ func (s SysConfigController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
configId := c.Param("configId")
if configId == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: configId is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: configId is empty"))
return
}
@@ -234,7 +234,7 @@ func (s SysConfigController) Refresh(c *gin.Context) {
func (s SysConfigController) ConfigKey(c *gin.Context) {
configKey := c.Param("configKey")
if configKey == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: configKey is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: configKey is empty"))
return
}
key := s.sysConfigService.FindValueByKey(configKey)
@@ -255,7 +255,7 @@ func (s SysConfigController) Export(c *gin.Context) {
rows, total := s.sysConfigService.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
}
@@ -319,7 +319,7 @@ func (s *SysConfigController) ConfigValue(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
}