fix(dingtalk): 登记钉钉知识库 409 space_registration_conflict —— 归档行占名 + 错误被吞
build / build (push) Successful in 57s
build / build (push) Successful in 57s
现象:点「公司级技术支持知识库」登记,返回 HTTP 409:space_registration_conflict。 根因(真数据取证): - source_connections.name 有唯一约束。这 3 个钉钉来源是**早期路径**登记的 (base_url 为空,id 与 dws 现在推导的确定性 id 不同): sc-18d10ca0… 公司级技术支持知识库 / sc-eaef0ec1… ATOM知识库 / sc-dc7cea1c… 技术诊断部知识沉淀。 - 上一步「去掉来源」只是归档下架,**名字仍占着唯一槽位** ⇒ 重新登记时 INSERT 撞 UNIQUE(name)。 - 处理器把**所有**存储层错误都翻译成同一个 409 space_registration_conflict(真实原因被吞)。 修复: - store:同名的另一行若**已归档** ⇒ 改名腾位(`…(已下架 · <短ID>)`,归档行不显示, 数据与审计一行不动);若**仍在使用** ⇒ ErrDingtalkSpaceNameTaken(绝不悄悄改名)。 同一个登记键解析到别的 root 时:在用中的行报 ErrDingtalkSpaceIdentityConflict; 归档过的行允许就地改写("去掉后再用 dws 重新获取"的语义)。 - httpapi:分类映射 —— space_name_conflict / space_identity_conflict(409 + 可行动 hint), 其余内部故障改回 500 space_registration_failed,不再伪装成冲突。 - web:读一次响应体同时取 error 与 hint,**hint 优先**;补两个新码的中文说明。 验证:真 postgres 集成测试复刻线上形态(旧行占名 + 归档 ⇒ 登记成功、旧行改名保持归档、 内容不丢;同名在用 ⇒ 拒绝;同键换 root 在用 ⇒ 拒绝、归档 ⇒ 就地改写);handler 分类测试 (409/409/500,且不回显内部细节);前端 hint 优先用例;make verify 15/15。
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
@@ -137,8 +138,22 @@ func registerDingtalkSpace(w http.ResponseWriter, r *http.Request, config Config
|
||||
connectionID, err := store.RegisterDingtalkSpace(r.Context(), postgresstore.RegisterDingtalkSpaceInput{
|
||||
RootKey: resolved.Key, Name: resolved.Name, SpaceID: spaceID, RootID: resolved.Root, Now: config.Now(),
|
||||
})
|
||||
if err != nil {
|
||||
writeJSON(w, http.StatusConflict, map[string]string{"error": "space_registration_conflict"})
|
||||
switch {
|
||||
case errors.Is(err, postgresstore.ErrDingtalkSpaceNameTaken):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{
|
||||
"error": "space_name_conflict",
|
||||
"hint": "已有同名来源仍在使用(未下架):请先在该来源卡片上「去掉来源」,或确认选的是同一个知识库空间。",
|
||||
})
|
||||
return
|
||||
case errors.Is(err, postgresstore.ErrDingtalkSpaceIdentityConflict):
|
||||
writeJSON(w, http.StatusConflict, map[string]string{
|
||||
"error": "space_identity_conflict",
|
||||
"hint": "这个登记键已指向另一个知识库空间:请刷新列表后重试;若仍失败,把该来源「去掉来源」后再登记。",
|
||||
})
|
||||
return
|
||||
case err != nil:
|
||||
// 真正的数据库/内部故障不能伪装成"冲突":换个 500 才有人去查日志。
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "space_registration_failed"})
|
||||
return
|
||||
}
|
||||
// First scan is enqueued right away: the new connection has no catalog
|
||||
|
||||
@@ -3,10 +3,13 @@ package httpapi
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
postgresstore "knowledge-governance/internal/platform/postgres"
|
||||
)
|
||||
|
||||
func TestDingtalkSessionProxyRelaysSidecarStates(t *testing.T) {
|
||||
@@ -92,3 +95,62 @@ func TestDingtalkSessionUnavailableWithoutSidecar(t *testing.T) {
|
||||
t.Fatalf("sidecar down status = %d, want 503", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// 登记失败必须能被分类:同名来源仍在用 / 同键换空间 是两种不同的冲突,
|
||||
// 数据库内部故障不能伪装成"冲突"(否则线上只能看到一个无细节的 409)。
|
||||
type fakeDingtalkRegistrar struct {
|
||||
*fakeJobStore
|
||||
err error
|
||||
}
|
||||
|
||||
func (f *fakeDingtalkRegistrar) RegisterDingtalkSpace(ctx context.Context, in postgresstore.RegisterDingtalkSpaceInput) (string, error) {
|
||||
if f.err != nil {
|
||||
return "", f.err
|
||||
}
|
||||
return in.ConnectionID(), nil
|
||||
}
|
||||
|
||||
func TestDingtalkSpaceRegistrationErrorClassification(t *testing.T) {
|
||||
sidecar := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPost && r.URL.Path == "/spaces/add" {
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"key": "support-kb", "name": "公司级技术支持知识库", "root": "root-support"})
|
||||
return
|
||||
}
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}))
|
||||
defer sidecar.Close()
|
||||
t.Setenv("DINGTALK_LOGIN_SIDECAR_URL", sidecar.URL)
|
||||
|
||||
call := func(storeErr error) (int, string) {
|
||||
store := &fakeDingtalkRegistrar{fakeJobStore: &fakeJobStore{}, err: storeErr}
|
||||
handler, err := NewHandler(Config{Store: store})
|
||||
if err != nil {
|
||||
t.Fatalf("NewHandler() error = %v", err)
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/dingtalk-spaces", strings.NewReader(`{"url":"https://alidocs.dingtalk.com/i/spaces/9JOK9EEOMLMWX4Q"}`))
|
||||
request.Header.Set("X-Governance-Action", "dingtalk-space")
|
||||
request.Header.Set("Idempotency-Key", "op-dingtalk-register")
|
||||
request = request.WithContext(context.WithValue(request.Context(), sessionKey{}, sessionIdentity{Username: "yaozhanghao", Role: roleAdmin}))
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, request)
|
||||
return recorder.Code, recorder.Body.String()
|
||||
}
|
||||
|
||||
status, body := call(postgresstore.ErrDingtalkSpaceNameTaken)
|
||||
if status != http.StatusConflict || !strings.Contains(body, "space_name_conflict") || !strings.Contains(body, "去掉来源") {
|
||||
t.Fatalf("同名冲突 = %d %s", status, body)
|
||||
}
|
||||
status, body = call(postgresstore.ErrDingtalkSpaceIdentityConflict)
|
||||
if status != http.StatusConflict || !strings.Contains(body, "space_identity_conflict") {
|
||||
t.Fatalf("同键冲突 = %d %s", status, body)
|
||||
}
|
||||
status, body = call(errors.New("connection refused: password=hunter2"))
|
||||
if status != http.StatusInternalServerError || !strings.Contains(body, "space_registration_failed") || strings.Contains(body, "hunter2") {
|
||||
t.Fatalf("内部故障 = %d %s(且不得回显细节)", status, body)
|
||||
}
|
||||
// 正常路径仍然成功(别把好的也拦掉)。
|
||||
status, body = call(nil)
|
||||
if status != http.StatusCreated || !strings.Contains(body, "connection_id") {
|
||||
t.Fatalf("正常登记 = %d %s", status, body)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user