fix: 修复资源不存在/已解散返回错误码及网关502问题
修复好友申请不存在时返回正确的 ErrNotFound 避免解引用 panic,新增通用函数判断群不存在或已解散并返回对应错误码,更新文档统一资源不存在类错误语义,避免服务端返回5xx错误。
This commit is contained in:
@@ -4427,6 +4427,10 @@ func serveIMGroupDetail(wrt http.ResponseWriter, req *http.Request, topic string
|
||||
return
|
||||
}
|
||||
if selfSub == nil || !(selfSub.ModeGiven & selfSub.ModeWant).IsJoiner() || !(selfSub.ModeGiven & selfSub.ModeWant).IsReader() {
|
||||
// 群不存在(404)或已解散(410)要与"存在但无权限"(403)区分开。
|
||||
if selfSub == nil && writeIMGroupMissingIfGone(wrt, req, principal.TenantID, topic) {
|
||||
return
|
||||
}
|
||||
writeIMError(wrt, req, http.StatusForbidden, 30005, imReasonPermissionDenied, "topic permission required", nil)
|
||||
return
|
||||
}
|
||||
@@ -9244,12 +9248,33 @@ func requireIMGroupMember(wrt http.ResponseWriter, req *http.Request, topic stri
|
||||
access = sub.ModeGiven & sub.ModeWant
|
||||
}
|
||||
if sub == nil || !access.IsJoiner() || (needRead && !access.IsReader()) {
|
||||
// 群不存在(404)或已解散(410)要与"存在但无权限"(403)区分开。
|
||||
if sub == nil && writeIMGroupMissingIfGone(wrt, req, principal.TenantID, topic) {
|
||||
return nil, nil, false
|
||||
}
|
||||
writeIMError(wrt, req, http.StatusForbidden, 30005, imReasonPermissionDenied, "topic permission required", nil)
|
||||
return nil, nil, false
|
||||
}
|
||||
return principal, sub, true
|
||||
}
|
||||
|
||||
// writeIMGroupMissingIfGone 在群不存在或已删除时写出 404/410 并返回 true;查询失败时返回 false 保持原错误路径。
|
||||
func writeIMGroupMissingIfGone(wrt http.ResponseWriter, req *http.Request, tenantID types.TenantID, topic string) bool {
|
||||
group, err := store.Topics.Get(tenantID, topic)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if group == nil {
|
||||
writeIMError(wrt, req, http.StatusNotFound, 30004, imReasonResourceNotFound, "group not found", nil)
|
||||
return true
|
||||
}
|
||||
if group.State == types.StateDeleted {
|
||||
writeIMError(wrt, req, http.StatusGone, 30004, imReasonGroupDissolved, "group dissolved", map[string]any{"topic": topic})
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func authenticateTopicReader(wrt http.ResponseWriter, req *http.Request, topic string) (*imPrincipal, string, bool) {
|
||||
principal, ok := authenticateIMHTTPBearer(wrt, req)
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user