fix: KGEmptyError 自定义异常 + CORS 环境变量配置

- qa_service 定义 KGEmptyError 异常,run_query 抛出;routers/query 精确捕获 → HTTP 400 + code 3002,移除字符串匹配
- main.py CORS 改为读 CORS_ORIGINS 环境变量:未设置时 wildcard + credentials=False(合规默认),设置后显式列表 + credentials=True
- .env.example 新增 CORS_ORIGINS 默认值(后端 + 前端 4 个域名)
This commit is contained in:
plf
2026-06-18 14:06:38 +08:00
parent 347f9dcae5
commit ebf27a6c3e
4 changed files with 31 additions and 8 deletions
+8 -1
View File
@@ -8,6 +8,13 @@ from datetime import datetime, timezone
from storage import file_store as fs
class KGEmptyError(Exception):
"""Raised when a QA request is made but the knowledge graph has no nodes."""
def __init__(self, msg: str = "Knowledge graph is empty. Index documents first."):
super().__init__(msg)
def run_query(question: str, history: list[dict]) -> dict:
from pipeline.qa_agent import run_qa
@@ -15,7 +22,7 @@ def run_query(question: str, history: list[dict]) -> dict:
edges = fs.load_kg_edges()
if not nodes:
raise ValueError("KG_EMPTY")
raise KGEmptyError()
start = time.time()
result = run_qa(question, history, nodes, edges)