fix(response): correct error status mapping and response structure
All checks were successful
Build Backend / build (push) Successful in 4m46s
Build Backend / build-docker (push) Successful in 5m33s

Map INVALID_CREDENTIALS to http.StatusBadRequest (400) instead of
http.StatusUnauthorized (401) to better align with client error semantics.

Update ErrorWithStringCode to return the HTTP status code in the 'code'
field and set 'data' to nil, removing the legacy compatibility workaround
that set 'code' to 0.

BREAKING CHANGE: Response structure for ErrorWithStringCode changed (Code
field now returns status code, Data is nil) and INVALID_CREDENTIALS status
changed from 401 to 400.
This commit is contained in:
lafay
2026-03-21 01:35:57 +08:00
parent 92babe509f
commit 57058b04da

View File

@@ -155,8 +155,10 @@ func statusCodeFromAppErrorCode(code string) int {
case "NOT_FOUND", "USER_NOT_FOUND", "POST_NOT_FOUND", "GROUP_NOT_FOUND", case "NOT_FOUND", "USER_NOT_FOUND", "POST_NOT_FOUND", "GROUP_NOT_FOUND",
"GROUP_REQUEST_NOT_FOUND", "SCHEDULE_COURSE_NOT_FOUND": "GROUP_REQUEST_NOT_FOUND", "SCHEDULE_COURSE_NOT_FOUND":
return http.StatusNotFound return http.StatusNotFound
case "UNAUTHORIZED", "INVALID_CREDENTIALS": case "UNAUTHORIZED":
return http.StatusUnauthorized return http.StatusUnauthorized
case "INVALID_CREDENTIALS":
return http.StatusBadRequest
case "FORBIDDEN", "NOT_GROUP_MEMBER", "NOT_GROUP_ADMIN", "NOT_GROUP_OWNER", case "FORBIDDEN", "NOT_GROUP_MEMBER", "NOT_GROUP_ADMIN", "NOT_GROUP_OWNER",
"USER_BANNED", "USER_BLOCKED", "MUTED", "MUTE_ALL_ENABLED", "USER_BANNED", "USER_BLOCKED", "MUTED", "MUTE_ALL_ENABLED",
"SCHEDULE_FORBIDDEN", "UNAUTHORIZED_NOTIFICATION": "SCHEDULE_FORBIDDEN", "UNAUTHORIZED_NOTIFICATION":
@@ -183,8 +185,8 @@ func statusCodeFromAppErrorCode(code string) int {
// ErrorWithStringCode 带字符串错误码的错误响应 // ErrorWithStringCode 带字符串错误码的错误响应
func ErrorWithStringCode(c *gin.Context, statusCode int, code string, message string) { func ErrorWithStringCode(c *gin.Context, statusCode int, code string, message string) {
c.JSON(statusCode, ResponseSnakeCase{ c.JSON(statusCode, ResponseSnakeCase{
Code: 0, // 为了兼容这里设为0实际错误码在message中体现 Code: statusCode,
Message: message, Message: message,
Data: code, Data: nil,
}) })
} }