Files
Agent-System/internal/errcode/code_test.go
T
weichen f3f23a3018 feat(errcode): 用稳定错误码记录 HTTP 与异步失败
失败响应此前不写日志,request_id 无法对照原因。注册表不覆盖处理器已经给出的 HTTP 状态。
2026-09-28 02:27:16 +00:00

65 lines
1.8 KiB
Go

package errcode
import (
"errors"
"strings"
"testing"
)
func TestRegistryUniqueAndPublic(t *testing.T) {
all := All()
if len(all) < 50 {
t.Fatalf("catalog too small: %d", len(all))
}
seen := map[string]struct{}{}
for _, d := range all {
if _, ok := seen[d.ID]; ok {
t.Fatalf("duplicate %s", d.ID)
}
seen[d.ID] = struct{}{}
if d.Message == "" || d.Guide == "" || d.Class == "" {
t.Fatalf("incomplete %+v", d)
}
if Sanitize(d.Message) != d.Message || Sanitize(d.Guide) != d.Guide {
t.Fatalf("public text redacted: %+v", d)
}
if strings.Contains(d.Guide, "/home/") || strings.Contains(strings.ToLower(d.Message), "password") {
t.Fatalf("leak %+v", d)
}
}
if _, ok := Lookup("INTERNAL"); !ok {
t.Fatal("missing INTERNAL")
}
if got := FromStatus(404).ID; got != "RESOURCE_NOT_FOUND" {
t.Fatalf("404 -> %s", got)
}
if got := FromLegacy(400, `{"error":"ERR_INVALID_PARAM","message":"无效的 JSON 请求体"}`).ID; got != "REQUEST_JSON_INVALID" {
t.Fatalf("json legacy -> %s", got)
}
if got := FromLegacy(400, "invalid request body: boom").ID; got != "REQUEST_BODY_INVALID" {
t.Fatalf("prefix -> %s", got)
}
}
func TestFromErrorSentinelAndTyped(t *testing.T) {
sentinel := errors.New("errcode test sentinel")
Bind(sentinel, "REQUEST_INVALID")
def, ok := FromError(sentinel)
if !ok || def.ID != "REQUEST_INVALID" {
t.Fatalf("sentinel %+v ok=%v", def, ok)
}
wrapped := New("DOC_BUSY", errors.New("编辑中"))
def, ok = FromError(wrapped)
if !ok || def.ID != "DOC_BUSY" {
t.Fatalf("typed %+v", def)
}
if Sanitize("pq: relation users password /home/x") != "" {
t.Fatal("expected redact")
}
}
func TestReportWithoutAuditor(t *testing.T) {
SetAuditor(nil)
Report(t.Context(), Event{Code: "INTERNAL", RequestID: "req_test", Upstream: "pq: secret"})
}