fix: unify handler response format in user_handler.go

- List/Get/Update/Delete users: standardize to {code, message, data} format
- UpdateUserStatus: standardize to {code, message} format
- handleError: standardize to {code, message} format (was {error: ...})
- All inline bad request errors now use {code: 400, message: ...} consistently
This commit is contained in:
2026-04-11 11:22:10 +08:00
parent 2cd76b2835
commit 8fe4669b97
2 changed files with 25 additions and 22 deletions

View File

@@ -417,14 +417,13 @@ func handleError(c *gin.Context, err error) {
// 优先尝试 ApplicationError内置 HTTP 状态码)
var appErr *apierrors.ApplicationError
if errors.As(err, &appErr) {
c.JSON(int(appErr.Code), gin.H{"error": appErr.Message})
c.JSON(int(appErr.Code), gin.H{"code": appErr.Code, "message": appErr.Message})
return
}
// 对普通 errors.New 按关键词推断语义,但只返回通用错误信息给客户端
msg := err.Error()
code := classifyErrorMessage(msg)
c.JSON(code, gin.H{"error": "服务器内部错误"})
httpCode := classifyErrorMessage(err.Error())
c.JSON(httpCode, gin.H{"code": httpCode, "message": "服务器内部错误"})
}
// classifyErrorMessage 通过错误信息关键词推断 HTTP 状态码,避免业务错误被 500 吞掉

View File

@@ -102,17 +102,21 @@ func (h *UserHandler) ListUsers(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"users": userResponses,
"total": total,
"offset": offset,
"limit": limit,
"code": 0,
"message": "success",
"data": gin.H{
"users": userResponses,
"total": total,
"offset": offset,
"limit": limit,
},
})
}
func (h *UserHandler) GetUser(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid user id"})
return
}
@@ -122,13 +126,13 @@ func (h *UserHandler) GetUser(c *gin.Context) {
return
}
c.JSON(http.StatusOK, toUserResponse(user))
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": toUserResponse(user)})
}
func (h *UserHandler) UpdateUser(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid user id"})
return
}
@@ -138,7 +142,7 @@ func (h *UserHandler) UpdateUser(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -160,13 +164,13 @@ func (h *UserHandler) UpdateUser(c *gin.Context) {
return
}
c.JSON(http.StatusOK, toUserResponse(user))
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": toUserResponse(user)})
}
func (h *UserHandler) DeleteUser(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid user id"})
return
}
@@ -175,13 +179,13 @@ func (h *UserHandler) DeleteUser(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "user deleted"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
}
func (h *UserHandler) UpdatePassword(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid user id"})
return
}
@@ -191,7 +195,7 @@ func (h *UserHandler) UpdatePassword(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -200,13 +204,13 @@ func (h *UserHandler) UpdatePassword(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "密码修改成功"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "密码修改成功"})
}
func (h *UserHandler) UpdateUserStatus(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid user id"})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid user id"})
return
}
@@ -215,7 +219,7 @@ func (h *UserHandler) UpdateUserStatus(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -230,7 +234,7 @@ func (h *UserHandler) UpdateUserStatus(c *gin.Context) {
case "disabled", "3":
status = domain.UserStatusDisabled
default:
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid status"})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid status"})
return
}
@@ -239,7 +243,7 @@ func (h *UserHandler) UpdateUserStatus(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "status updated"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
}
func (h *UserHandler) GetUserRoles(c *gin.Context) {