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

@@ -20,14 +20,14 @@ func ErrorCatch() gin.HandlerFunc {
// 返回错误响应给客户端
if config.Env() == "prod" {
c.JSON(500, resp.CodeMsg(500, "Internal Server Errors"))
c.JSON(500, resp.CodeMsg(500001, "Internal Server Errors"))
} else {
// 通过实现 error 接口的 Error() 方法自定义错误类型进行捕获
switch v := err.(type) {
case error:
c.JSON(500, resp.CodeMsg(500, v.Error()))
c.JSON(500, resp.CodeMsg(500001, v.Error()))
default:
c.JSON(500, resp.CodeMsg(500, fmt.Sprint(err)))
c.JSON(500, resp.CodeMsg(500001, fmt.Sprint(err)))
}
}