fix: list_collections 异常向上传播而非静默吞掉

This commit is contained in:
2026-07-10 15:28:03 +08:00
parent 3308636086
commit 2e95f68864
4 changed files with 9 additions and 8 deletions
+1 -1
View File
@@ -11,7 +11,7 @@ Markdown 文档向量数据库 — 将 .md 文件分块 → 嵌入 → 存入 Ch
```bash ```bash
# --- 安装与测试 --- # --- 安装与测试 ---
uv sync # 安装依赖(lockfile 已锁定 CUDA torch uv sync # 安装依赖(lockfile 已锁定 CUDA torch
uv run pytest tests/ -v # 全部测试 (46 个) uv run pytest tests/ -v # 全部测试 (115+ 个)
uv run pytest tests/test_api.py -v # 单个测试模块 uv run pytest tests/test_api.py -v # 单个测试模块
uv run pytest tests/ -v -k "test_search" # 按名称过滤 uv run pytest tests/ -v -k "test_search" # 按名称过滤
+1 -1
View File
@@ -242,7 +242,7 @@ md-vector-db/
## 测试 ## 测试
```bash ```bash
uv run pytest tests/ -v # 全部测试 (46 个) uv run pytest tests/ -v # 全部测试 (115+ 个)
uv run pytest tests/test_embedder.py -v # 嵌入器测试 uv run pytest tests/test_embedder.py -v # 嵌入器测试
uv run pytest tests/ -v -k "search" # 按名称过滤 uv run pytest tests/ -v -k "search" # 按名称过滤
uv run pytest tests/ -v --cov=src --cov-report=term-missing # 覆盖率 uv run pytest tests/ -v --cov=src --cov-report=term-missing # 覆盖率
+4
View File
@@ -106,7 +106,11 @@ def list_collections(
state: AppState = Depends(get_state), state: AppState = Depends(get_state),
_: bool = Depends(verify_api_key), _: bool = Depends(verify_api_key),
): ):
try:
return {"collections": state.list_collections_with_stats()} return {"collections": state.list_collections_with_stats()}
except Exception:
logger.exception("列出集合失败")
raise HTTPException(status_code=500, detail="服务器内部错误")
@app.post("/api/v1/ingest") @app.post("/api/v1/ingest")
-3
View File
@@ -54,11 +54,8 @@ class AppState:
def list_collections_with_stats(self) -> list[dict]: def list_collections_with_stats(self) -> list[dict]:
"""列出所有 collection 及其统计(直接从 ChromaDB 查询).""" """列出所有 collection 及其统计(直接从 ChromaDB 查询)."""
result = [] result = []
try:
for coll in self.db.list_collections(): for coll in self.db.list_collections():
result.append({"name": coll.name, "count": coll.count()}) result.append({"name": coll.name, "count": coll.count()})
except Exception:
logger.exception("列出集合失败")
return result return result
def is_healthy(self) -> dict: def is_healthy(self) -> dict: