fix: try/except around DB queries (Agent 2 review)

This commit is contained in:
yumoqing 2026-07-30 17:43:18 +08:00
parent a440887028
commit 5b1fb8e564
2 changed files with 40 additions and 34 deletions

View File

@ -10,26 +10,29 @@ db = DBPools()
dbname = get_module_dbname('rag')
rows = []
async with db.sqlorContext(dbname) as sor:
if folder_id:
recs = await sor.sqlExe(
"SELECT id, file_name, file_type, file_size, file_path, status, chunk_count, created_at FROM documents WHERE kb_id=${kb_id}$ AND folder_id=${folder_id}$ ORDER BY created_at DESC",
{"kb_id": kb_id, "folder_id": folder_id})
else:
recs = await sor.sqlExe(
"SELECT id, file_name, file_type, file_size, file_path, status, chunk_count, created_at FROM documents WHERE kb_id=${kb_id}$ AND (folder_id IS NULL OR folder_id='') ORDER BY created_at DESC",
{"kb_id": kb_id})
for r in recs:
rows.append({
"id": r.id,
"file_name": r.file_name,
"file_type": r.file_type or '',
"file_size": r.file_size or 0,
"file_path": r.file_path or '',
"status": r.status or '',
"chunk_count": r.chunk_count or 0,
"created_at": str(r.created_at)[:16] if r.created_at else ''
})
try:
async with db.sqlorContext(dbname) as sor:
if folder_id:
recs = await sor.sqlExe(
"SELECT id, file_name, file_type, file_size, file_path, status, chunk_count, created_at FROM documents WHERE kb_id=${kb_id}$ AND folder_id=${folder_id}$ ORDER BY created_at DESC",
{"kb_id": kb_id, "folder_id": folder_id})
else:
recs = await sor.sqlExe(
"SELECT id, file_name, file_type, file_size, file_path, status, chunk_count, created_at FROM documents WHERE kb_id=${kb_id}$ AND (folder_id IS NULL OR folder_id='') ORDER BY created_at DESC",
{"kb_id": kb_id})
for r in recs:
rows.append({
"id": r.id,
"file_name": r.file_name,
"file_type": r.file_type or '',
"file_size": r.file_size or 0,
"file_path": r.file_path or '',
"status": r.status or '',
"chunk_count": r.chunk_count or 0,
"created_at": str(r.created_at)[:16] if r.created_at else ''
})
except:
return json.dumps({"widgettype": "Text", "options": {"text": "加载文件列表失败", "cfontsize": 14, "color": "#e74c3c", "halign": "center", "marginTop": "40px"}}, ensure_ascii=False)
def fmt_size(sz):
if sz < 1024: return str(sz) + "B"

View File

@ -3,18 +3,21 @@ id = ns.get('id')
kb_id = ns.get('kb_id', '')
db = DBPools()
dbname = get_module_dbname('rag')
async with db.sqlorContext(dbname) as sor:
if not id:
recs = await sor.sqlExe(
"SELECT id, '' as parentid, content as label FROM document_chunks WHERE kb_id=${kb_id}$ AND chunk_type='directory' AND (description IS NULL OR description='') ORDER BY content",
{"kb_id": kb_id})
result = [{"id": "__root__", "parentid": "", "label": "📁 根目录"}]
for r in recs:
result.append({"id": r.id, "parentid": r.parentid, "label": r.label})
return result
else:
recs = await sor.sqlExe(
"SELECT id, description as parentid, content as label FROM document_chunks WHERE kb_id=${kb_id}$ AND chunk_type='directory' AND description=${id}$ ORDER BY content",
{"kb_id": kb_id, "id": id})
return [dict(r) for r in recs]
try:
async with db.sqlorContext(dbname) as sor:
if not id:
recs = await sor.sqlExe(
"SELECT id, '' as parentid, content as label FROM document_chunks WHERE kb_id=${kb_id}$ AND chunk_type='directory' AND (description IS NULL OR description='') ORDER BY content",
{"kb_id": kb_id})
result = [{"id": "__root__", "parentid": "", "label": "📁 根目录"}]
for r in recs:
result.append({"id": r.id, "parentid": r.parentid, "label": r.label})
return result
else:
recs = await sor.sqlExe(
"SELECT id, description as parentid, content as label FROM document_chunks WHERE kb_id=${kb_id}$ AND chunk_type='directory' AND description=${id}$ ORDER BY content",
{"kb_id": kb_id, "id": id})
return [dict(r) for r in recs]
except:
return [{"id": "__root__", "parentid": "", "label": "📁 根目录(加载失败)"}]
return []