package kit import ( "context" "errors" "fmt" "strings" "testing" "go.mongodb.org/mongo-driver/mongo" ) func TestUpstreamHidesRawError(t *testing.T) { cases := []struct { err error kind string }{ {errors.New(`pq: relation "site_info" does not exist (host=10.0.0.5:5432)`), "数据源返回错误"}, {fmt.Errorf("query: %w", context.DeadlineExceeded), "请求超时"}, {fmt.Errorf("find: %w", mongo.ErrNoDocuments), "没有找到对应记录"}, } for _, c := range cases { text := UpstreamText("测试数据源", c.err) if strings.Contains(text, "10.0.0.5") || strings.Contains(text, "relation") || strings.Contains(text, "mongo") { t.Errorf("错误说明泄露了原始错误:%s", text) } if !strings.Contains(text, c.kind) || !strings.Contains(text, "错误编号") { t.Errorf("错误说明应包含类别 %s 与错误编号:%s", c.kind, text) } } }