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

@@ -54,7 +54,7 @@ func (s *NeActionController) PushFile(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
}
@@ -127,7 +127,7 @@ func (s *NeActionController) PullFile(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -199,7 +199,7 @@ func (s *NeActionController) PullDirZip(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -280,7 +280,7 @@ func (s *NeActionController) ViewFile(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -341,7 +341,7 @@ func (s *NeActionController) Files(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -413,7 +413,7 @@ func (s *NeActionController) Service(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
}

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
}

View File

@@ -50,7 +50,7 @@ func (s NeConfigBackupController) Download(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
}
@@ -80,7 +80,7 @@ func (s NeConfigBackupController) Edit(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
}
@@ -110,7 +110,7 @@ func (s NeConfigBackupController) 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
}
@@ -144,7 +144,7 @@ func (s NeConfigBackupController) 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
}
if !strings.HasSuffix(body.Path, ".zip") {
@@ -181,7 +181,7 @@ func (s NeConfigBackupController) Export(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
}
// 查网元

View File

@@ -75,11 +75,11 @@ func (s NeHostController) Add(c *gin.Context) {
var body model.NeHost
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.ID == 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: id not is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id not is empty"))
return
}
@@ -116,11 +116,11 @@ func (s NeHostController) Edit(c *gin.Context) {
var body model.NeHost
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.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
}
@@ -196,7 +196,7 @@ func (s NeHostController) Test(c *gin.Context) {
var body model.NeHost
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
}
@@ -278,7 +278,7 @@ func (s NeHostController) Cmd(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
}
@@ -358,7 +358,7 @@ func (s NeHostController) CheckBySSH(c *gin.Context) {
var body model.NeHost
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
}
@@ -460,11 +460,11 @@ func (s NeHostController) AuthorizedBySSH(c *gin.Context) {
var body model.NeHost
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.AuthMode == "2" {
c.JSON(400, resp.CodeMsg(40010, "bind err: auth mode not equals 2"))
c.JSON(422, resp.CodeMsg(422002, "bind err: auth mode not equals 2"))
return
}

View File

@@ -41,7 +41,7 @@ func (s NeHostCmdController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := parse.Number(c.Param("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
}
@@ -63,7 +63,7 @@ func (s NeHostCmdController) Add(c *gin.Context) {
var body model.NeHostCmd
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
}
@@ -93,7 +93,7 @@ func (s NeHostCmdController) Edit(c *gin.Context) {
var body model.NeHostCmd
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
}
@@ -130,7 +130,7 @@ func (s NeHostCmdController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Param("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
}

View File

@@ -58,7 +58,7 @@ func (s NeInfoController) State(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -122,7 +122,7 @@ func (s NeInfoController) NeTypeAndID(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -160,7 +160,7 @@ func (s NeInfoController) ListAll(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -202,7 +202,7 @@ func (s NeInfoController) Para5GFileWrite(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
}
@@ -224,7 +224,7 @@ func (s NeInfoController) OAMFileRead(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -249,7 +249,7 @@ func (s NeInfoController) OAMFileWrite(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,7 +312,7 @@ func (s NeInfoController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := parse.Number(c.Param("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
}
@@ -345,11 +345,11 @@ func (s NeInfoController) Add(c *gin.Context) {
err := c.ShouldBindBodyWithJSON(&body)
if 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.ID != 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: id not is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: id not is empty"))
return
}
@@ -438,11 +438,11 @@ func (s NeInfoController) Edit(c *gin.Context) {
err := c.ShouldBindBodyWithJSON(&body)
if 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.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
}
@@ -549,7 +549,7 @@ func (s NeInfoController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Param("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
}

View File

@@ -66,7 +66,7 @@ func (s *NeLicenseController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := parse.Number(c.Param("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
}
@@ -102,7 +102,7 @@ func (s *NeLicenseController) NeTypeAndID(c *gin.Context) {
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -137,7 +137,8 @@ func (s *NeLicenseController) Code(c *gin.Context) {
NeId string `form:"neId" binding:"required"`
}
if err := c.ShouldBindQuery(&querys); 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
}
@@ -183,7 +184,8 @@ func (s *NeLicenseController) Change(c *gin.Context) {
var body model.NeLicense
err := c.ShouldBindBodyWithJSON(&body)
if err != nil || body.LicensePath == "" {
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
}

View File

@@ -55,7 +55,7 @@ func (s NeSoftwareController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := parse.Number(c.Param("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
}
@@ -86,11 +86,11 @@ func (s NeSoftwareController) Add(c *gin.Context) {
var body model.NeSoftware
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.Path == "" || body.ID > 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: path is empty or id is not empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: path is empty or id is not empty"))
return
}
// 找到已存在的删除后重新添加
@@ -130,11 +130,11 @@ func (s NeSoftwareController) Edit(c *gin.Context) {
var body model.NeSoftware
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.Path == "" || body.ID == 0 {
c.JSON(400, resp.CodeMsg(40010, "bind err: path or id is empty"))
c.JSON(422, resp.CodeMsg(422002, "bind err: path or id is empty"))
return
}
@@ -171,7 +171,7 @@ func (s NeSoftwareController) Remove(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Param("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
}
@@ -209,7 +209,7 @@ func (s NeSoftwareController) NewNeVersion(c *gin.Context) {
var body model.NeSoftware
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
}

View File

@@ -40,7 +40,7 @@ func (s *NeVersionController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := parse.Number(c.Param("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
}
@@ -77,7 +77,7 @@ func (s *NeVersionController) Operate(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
}