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:
@@ -50,7 +50,7 @@ func (s *MonitorController) Load(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
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ func (s SysCacheController) Keys(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
|
||||
}
|
||||
|
||||
@@ -96,7 +96,7 @@ func (s SysCacheController) Value(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
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ func (s SysCacheController) CleanKeys(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
|
||||
}
|
||||
if constants.CACHE_TOKEN_DEVICE == query.CacheName {
|
||||
@@ -196,7 +196,7 @@ func (s SysCacheController) CleanValue(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
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ func (s *SysJobController) Info(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
jobId := parse.Number(c.Param("jobId"))
|
||||
if jobId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: jobId is empty"))
|
||||
c.JSON(422, resp.CodeMsg(422002, "bind err: jobId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -87,11 +87,11 @@ func (s *SysJobController) Add(c *gin.Context) {
|
||||
var body model.SysJob
|
||||
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.JobId > 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: jobId not is empty"))
|
||||
c.JSON(422, resp.CodeMsg(422002, "bind err: jobId not is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -143,11 +143,11 @@ func (s *SysJobController) Edit(c *gin.Context) {
|
||||
var body model.SysJob
|
||||
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.JobId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: jobId is empty"))
|
||||
c.JSON(422, resp.CodeMsg(422002, "bind err: jobId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -229,7 +229,7 @@ func (s *SysJobController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
jobId := c.Param("jobId")
|
||||
if jobId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: jobId is empty"))
|
||||
c.JSON(422, resp.CodeMsg(422002, "bind err: jobId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -261,7 +261,7 @@ func (s *SysJobController) Status(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
|
||||
}
|
||||
|
||||
@@ -299,7 +299,7 @@ func (s *SysJobController) Run(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
jobId := parse.Number(c.Param("jobId"))
|
||||
if jobId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: jobId is empty"))
|
||||
c.JSON(422, resp.CodeMsg(422002, "bind err: jobId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ func (s *SysJobLogController) List(c *gin.Context) {
|
||||
func (s *SysJobLogController) Info(c *gin.Context) {
|
||||
logId := parse.Number(c.Param("logId"))
|
||||
if logId <= 0 {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: logId is empty"))
|
||||
c.JSON(422, resp.CodeMsg(422002, "bind err: logId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ func (s *SysJobLogController) Remove(c *gin.Context) {
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
logId := c.Param("logId")
|
||||
if logId == "" {
|
||||
c.JSON(400, resp.CodeMsg(40010, "bind err: logId is empty"))
|
||||
c.JSON(422, resp.CodeMsg(422002, "bind err: logId is empty"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -143,13 +143,6 @@ func (s *SysJobLogController) Export(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// rows := s.sysJobLogService.SelectJobLogList(model.SysJobLog{})
|
||||
if len(rows) <= 0 {
|
||||
// 导出数据记录为空
|
||||
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||||
return
|
||||
}
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysJobLog) {
|
||||
for i := range *arr {
|
||||
|
||||
Reference in New Issue
Block a user