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

@@ -43,7 +43,7 @@ func (s *TraceTaskController) List(c *gin.Context) {
func (s *TraceTaskController) Info(c *gin.Context) {
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
}
@@ -61,19 +61,18 @@ func (s *TraceTaskController) Info(c *gin.Context) {
func (s *TraceTaskController) Add(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var body model.TraceTask
err := c.ShouldBindBodyWithJSON(&body)
if err != nil {
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
}
body.CreateBy = reqctx.LoginUserToUserName(c)
if err = s.traceTaskService.Insert(body); err != nil {
if err := s.traceTaskService.Insert(body); err != nil {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, err.Error())))
return
}
@@ -87,7 +86,7 @@ func (s *TraceTaskController) 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
}
@@ -112,12 +111,12 @@ func (s *TraceTaskController) Remove(c *gin.Context) {
//
// GET /filePull
func (s *TraceTaskController) FilePull(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
var querys struct {
TraceId string `form:"traceId" 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
}