fix(reconciliation): 失败提示改中文并去掉绑定页 090
build / build (push) Successful in 1m51s

- 页面 9 个出口统一走 reconciliationMessages:四个内部码不再进入 DOM,未知码回退中文;批次行/阶段码同样翻译。
- 绑定页加载与点击前读 /api/v1/kb-bindings 预检:测试环境未配置当场提示且不发 POST;未启用优先于绑定缺失。
- 运行时读按上游 401/403 与连接失败拆成 runtime_key_forbidden / runtime_unreachable,保留 404 与未配置语义。
- EnvironmentLabel 与前端环境标签改「测试/生产知识库」,不再出现 090。
- 验:make verify 17 项全绿;go test 三包、web 656 用例、typecheck 通过。
This commit is contained in:
Zhero & Rin
2026-12-02 10:34:49 +08:00
committed by 姚章浩
parent 8210af7847
commit 6a12196bf3
22 changed files with 484 additions and 40 deletions
+4
View File
@@ -46,6 +46,10 @@ func writeReconciliationError(w http.ResponseWriter, err error) {
writeJSON(w, http.StatusNotFound, map[string]string{"error": "reconciliation record not found"})
case errors.Is(err, reconciliation.ErrActionConflict), errors.Is(err, reconciliation.ErrActionNotAllowed):
writeJSON(w, http.StatusConflict, map[string]string{"error": "reconciliation_state_changed", "code": "reconciliation_state_changed"})
case errors.Is(err, reconciliation.ErrRuntimeForbidden):
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "runtime_key_forbidden", "code": "runtime_key_forbidden"})
case errors.Is(err, reconciliation.ErrRuntimeUnreachable):
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "runtime_unreachable", "code": "runtime_unreachable"})
case errors.Is(err, reconciliation.ErrRuntimeUnavailable):
writeJSON(w, http.StatusServiceUnavailable, map[string]string{"error": "runtime_read_unavailable", "code": "runtime_read_unavailable"})
case errors.Is(err, reconciliation.ErrRemoteFailed):
+22
View File
@@ -144,3 +144,25 @@ func TestReconciliationProgressRejectsMalformedDocumentIDWith400(t *testing.T) {
}
}
}
func TestReconciliationProgressClassifiesRuntimeFailures(t *testing.T) {
cases := []struct {
err error
code string
}{
{reconciliation.ErrRuntimeForbidden, "runtime_key_forbidden"},
{reconciliation.ErrRuntimeUnreachable, "runtime_unreachable"},
{reconciliation.ErrRuntimeUnavailable, "runtime_read_unavailable"},
}
for _, tc := range cases {
handler, err := NewHandler(Config{Store: &fakeJobStore{}, Reconciliation: &reconciliationServiceStub{err: tc.err}})
if err != nil {
t.Fatal(err)
}
recorder := httptest.NewRecorder()
handler.ServeHTTP(recorder, reconciliationRequest(http.MethodGet, "/api/v1/reconciliation/documents/doc-1/progress", roleViewer, ""))
if recorder.Code != http.StatusServiceUnavailable || !strings.Contains(recorder.Body.String(), tc.code) {
t.Fatalf("%v = %d %s, want 503 %s", tc.err, recorder.Code, recorder.Body.String(), tc.code)
}
}
}
+2 -2
View File
@@ -30,9 +30,9 @@ func ValidEnvironment(value string) bool {
func EnvironmentLabel(value string) string {
if value == EnvProduction {
return "生产 090"
return "生产知识库"
}
return "测试 090"
return "测试知识库"
}
var (
+9
View File
@@ -221,3 +221,12 @@ func TestAuditTextBoundsWithoutSplittingUTF8(t *testing.T) {
t.Fatal("blank audit source must stay empty, never a placeholder")
}
}
func TestEnvironmentLabelNamesTheKnowledgeBaseNotTheOldVersion(t *testing.T) {
if got := EnvironmentLabel(EnvTest); got != "测试知识库" {
t.Fatalf("test label = %q", got)
}
if got := EnvironmentLabel(EnvProduction); got != "生产知识库" {
t.Fatalf("production label = %q", got)
}
}
+7 -2
View File
@@ -2,6 +2,7 @@ package reconciliation
import (
"context"
"net/http"
"strings"
"time"
@@ -96,10 +97,14 @@ func (p *RuntimeProvider) DocumentProgress(ctx context.Context, documentID strin
}
detail, err := client.DocumentProgress(ctx, documentID)
if err != nil {
if weknora.IsNotFound(err) {
switch {
case weknora.IsNotFound(err):
return governance.ReconciliationProgress{}, err
case weknora.StatusCode(err) == http.StatusUnauthorized || weknora.StatusCode(err) == http.StatusForbidden:
return governance.ReconciliationProgress{}, ErrRuntimeForbidden
default:
return governance.ReconciliationProgress{}, ErrRuntimeUnreachable
}
return governance.ReconciliationProgress{}, ErrRuntimeUnavailable
}
progress := governance.ReconciliationProgress{
DocumentID: detail.Document.ID, ParseStatus: detail.Document.ParseStatus,
+42
View File
@@ -107,3 +107,45 @@ func TestRuntimeProviderDropsSensitiveUpstreamFields(t *testing.T) {
t.Fatalf("projection dropped real fields: %#v", progress)
}
}
func TestRuntimeProviderClassifiesForbiddenAndUnreachable(t *testing.T) {
cases := []struct {
name string
status int
want error
}{
{"forbidden", http.StatusForbidden, ErrRuntimeForbidden},
{"unauthorized", http.StatusUnauthorized, ErrRuntimeForbidden},
{"upstream-500", http.StatusInternalServerError, ErrRuntimeUnreachable},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(tc.status)
}))
defer server.Close()
provider := NewRuntimeProvider(RuntimeConfig{
BaseURL: func(context.Context) string { return server.URL },
APIKey: "runtime-key", Timeout: 5 * time.Second,
})
if _, err := provider.DocumentProgress(context.Background(), "doc-1"); !errors.Is(err, tc.want) {
t.Fatalf("status %d err=%v, want %v", tc.status, err, tc.want)
}
})
}
}
func TestRuntimeProviderClassifiesClosedListenerAsUnreachable(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(w).Encode(map[string]any{"success": true, "data": map[string]any{}})
}))
url := server.URL
server.Close()
provider := NewRuntimeProvider(RuntimeConfig{
BaseURL: func(context.Context) string { return url },
APIKey: "runtime-key", Timeout: 2 * time.Second,
})
if _, err := provider.DocumentProgress(context.Background(), "doc-1"); !errors.Is(err, ErrRuntimeUnreachable) {
t.Fatalf("closed listener err=%v, want %v", err, ErrRuntimeUnreachable)
}
}
+7
View File
@@ -21,6 +21,10 @@ var (
ErrActionNotAllowed = errors.New("reconciliation action not allowed")
ErrRemoteFailed = errors.New("reconciliation remote call failed")
ErrRuntimeUnavailable = errors.New("weknora runtime read unavailable")
// ErrRuntimeForbidden / ErrRuntimeUnreachable 把「Key 无权限」与「连不上」
// 从 ErrRuntimeUnavailable 里拆出来:页面要给两种不同的中文,不能再折叠。
ErrRuntimeForbidden = errors.New("weknora runtime key forbidden")
ErrRuntimeUnreachable = errors.New("weknora runtime read unreachable")
)
type Store interface {
@@ -326,6 +330,9 @@ func (s *Service) Progress(ctx context.Context, documentID string) (governance.R
switch {
case err == nil:
return progress, nil
case errors.Is(err, ErrRuntimeForbidden), errors.Is(err, ErrRuntimeUnreachable), errors.Is(err, ErrRuntimeUnavailable):
// Provider 已经分好类;这里若再折叠,页面就永远看不到两种不同原因。
return governance.ReconciliationProgress{}, err
case weknora.StatusCode(err) == 404:
return governance.ReconciliationProgress{}, ErrNotFound
default:
+15
View File
@@ -580,3 +580,18 @@ func TestCreateRunEnqueuesJobWithinJobIDLimit(t *testing.T) {
t.Fatalf("payload = %s", request.Payload)
}
}
type progressRuntimeStub struct{ err error }
func (s progressRuntimeStub) DocumentProgress(context.Context, string) (governance.ReconciliationProgress, error) {
return governance.ReconciliationProgress{}, s.err
}
func TestProgressPassesProviderClassificationInsteadOfRefolding(t *testing.T) {
for _, want := range []error{ErrRuntimeForbidden, ErrRuntimeUnreachable, ErrRuntimeUnavailable} {
service := &Service{runtime: progressRuntimeStub{err: want}}
if _, err := service.Progress(context.Background(), "doc-1"); !errors.Is(err, want) {
t.Fatalf("Progress err=%v, want %v", err, want)
}
}
}