From 57058b04da3d93485f3ec5e536782f856c3bb4e1 Mon Sep 17 00:00:00 2001 From: lafay <2021211506@stu.hit.edu.cn> Date: Sat, 21 Mar 2026 01:35:57 +0800 Subject: [PATCH] fix(response): correct error status mapping and response structure 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. --- internal/pkg/response/response.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/internal/pkg/response/response.go b/internal/pkg/response/response.go index 7be7275..1514fc3 100644 --- a/internal/pkg/response/response.go +++ b/internal/pkg/response/response.go @@ -155,8 +155,10 @@ func statusCodeFromAppErrorCode(code string) int { case "NOT_FOUND", "USER_NOT_FOUND", "POST_NOT_FOUND", "GROUP_NOT_FOUND", "GROUP_REQUEST_NOT_FOUND", "SCHEDULE_COURSE_NOT_FOUND": return http.StatusNotFound - case "UNAUTHORIZED", "INVALID_CREDENTIALS": + case "UNAUTHORIZED": return http.StatusUnauthorized + case "INVALID_CREDENTIALS": + return http.StatusBadRequest case "FORBIDDEN", "NOT_GROUP_MEMBER", "NOT_GROUP_ADMIN", "NOT_GROUP_OWNER", "USER_BANNED", "USER_BLOCKED", "MUTED", "MUTE_ALL_ENABLED", "SCHEDULE_FORBIDDEN", "UNAUTHORIZED_NOTIFICATION": @@ -183,8 +185,8 @@ func statusCodeFromAppErrorCode(code string) int { // ErrorWithStringCode 带字符串错误码的错误响应 func ErrorWithStringCode(c *gin.Context, statusCode int, code string, message string) { c.JSON(statusCode, ResponseSnakeCase{ - Code: 0, // 为了兼容,这里设为0,实际错误码在message中体现 + Code: statusCode, Message: message, - Data: code, + Data: nil, }) }