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

@@ -64,7 +64,7 @@ func RateLimit(option LimitOption) gin.HandlerFunc {
if option.Type == LIMIT_USER {
loginUser, err := reqctx.LoginUser(c)
if err != nil {
c.JSON(401, resp.CodeMsg(40003, err.Error()))
c.JSON(401, resp.CodeMsg(401002, "invalid login user information"))
c.Abort() // 停止执行后续的处理函数
return
}
@@ -80,13 +80,13 @@ func RateLimit(option LimitOption) gin.HandlerFunc {
// 在Redis查询并记录请求次数
rateCount, err := redis.RateLimit("", limitKey, option.Time, option.Count)
if err != nil {
c.JSON(200, resp.CodeMsg(4013, "访问过于频繁,请稍候再试"))
c.JSON(200, resp.ErrMsg("access too often, please try again later"))
c.Abort() // 停止执行后续的处理函数
return
}
rateTime, err := redis.GetExpire("", limitKey)
if err != nil {
c.JSON(200, resp.CodeMsg(4013, "访问过于频繁,请稍候再试"))
c.JSON(200, resp.ErrMsg("access too often, please try again later"))
c.Abort() // 停止执行后续的处理函数
return
}
@@ -97,7 +97,7 @@ func RateLimit(option LimitOption) gin.HandlerFunc {
c.Header("X-RateLimit-Reset", fmt.Sprintf("%d", time.Now().Unix()+rateTime)) // 重置时间戳
if rateCount >= option.Count {
c.JSON(200, resp.CodeMsg(4013, "访问过于频繁,请稍候再试"))
c.JSON(200, resp.ErrMsg("access too often, please try again later"))
c.Abort() // 停止执行后续的处理函数
return
}