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

@@ -46,7 +46,7 @@ func (s NeConfigController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := parse.Number(c.Query("id"))
if id <= 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty"))
return
}
@@ -72,14 +72,14 @@ func (s NeConfigController) Add(c *gin.Context) {
var body model.NeConfig
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
}
// 将json数据转字符串存储
paramDataByte, err := json.Marshal(body.ParamData)
if err != nil {
c.JSON(400, resp.CodeMsg(400, err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
body.ParamJson = string(paramDataByte)
@@ -100,7 +100,7 @@ func (s NeConfigController) Edit(c *gin.Context) {
var body model.NeConfig
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
}
@@ -115,7 +115,7 @@ func (s NeConfigController) Edit(c *gin.Context) {
// 将json数据转字符串存储
paramDataByte, err := json.Marshal(body.ParamData)
if err != nil {
c.JSON(400, resp.CodeMsg(400, err.Error()))
c.JSON(200, resp.ErrMsg(err.Error()))
return
}
body.ParamJson = string(paramDataByte)
@@ -135,7 +135,7 @@ func (s NeConfigController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Query("id")
if id == "" {
c.JSON(400, resp.CodeMsg(40010, "bind err: id is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id is empty"))
return
}
@@ -170,10 +170,9 @@ func (s NeConfigController) Remove(c *gin.Context) {
// @Description Network Element Parameter Configuration Available Attribute Values List Specify Network Element Type All Unpaged
// @Router /ne/config/list/{neType} [get]
func (s NeConfigController) ListByNeType(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
neType := c.Param("neType")
if neType == "" {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(422, resp.CodeMsg(422002, "bind err: neType is empty"))
return
}
data := s.neConfigService.FindByNeType(neType)
@@ -204,7 +203,7 @@ func (s NeConfigController) DataInfo(c *gin.Context) {
}
if err := c.ShouldBindQuery(&query); 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
}
@@ -259,7 +258,7 @@ func (s NeConfigController) DataEdit(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
}
@@ -312,20 +311,20 @@ func (s NeConfigController) DataAdd(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
}
// 检查是否array
info := s.neConfigService.FindByNeTypeAndParamName(body.NeType, body.ParamName)
if info.ParamType != "array" {
c.JSON(400, resp.CodeMsg(400, "this attribute does not support adding"))
c.JSON(200, resp.ErrMsg("this attribute does not support adding"))
return
}
// 必须含有index
_, idxOk := body.ParamData["index"]
if info.ParamType == "array" && !idxOk {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
c.JSON(200, resp.ErrMsg("array data must contain index"))
return
}
@@ -369,14 +368,15 @@ func (s NeConfigController) DataRemove(c *gin.Context) {
Loc string `form:"loc" binding:"required"` // 与数据对象内index一致,有多层时划分嵌套层(index/subParamName/index)
}
if err := c.ShouldBindQuery(&query); err != nil {
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
c.JSON(422, resp.CodeMsg(422001, errMsgs))
return
}
// 检查是否array
info := s.neConfigService.FindByNeTypeAndParamName(query.NeType, query.ParamName)
if info.ParamType != "array" {
c.JSON(400, resp.CodeMsg(400, "this attribute does not support adding"))
c.JSON(200, resp.ErrMsg("this attribute does not support adding"))
return
}