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') dbname = get_module_dbname('rag')
rows = [] rows = []
async with db.sqlorContext(dbname) as sor: try:
if folder_id: async with db.sqlorContext(dbname) as sor:
recs = await sor.sqlExe( if folder_id:
"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", recs = await sor.sqlExe(
{"kb_id": kb_id, "folder_id": folder_id}) "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",
else: {"kb_id": kb_id, "folder_id": folder_id})
recs = await sor.sqlExe( else:
"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", recs = await sor.sqlExe(
{"kb_id": kb_id}) "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",
for r in recs: {"kb_id": kb_id})
rows.append({ for r in recs:
"id": r.id, rows.append({
"file_name": r.file_name, "id": r.id,
"file_type": r.file_type or '', "file_name": r.file_name,
"file_size": r.file_size or 0, "file_type": r.file_type or '',
"file_path": r.file_path or '', "file_size": r.file_size or 0,
"status": r.status or '', "file_path": r.file_path or '',
"chunk_count": r.chunk_count or 0, "status": r.status or '',
"created_at": str(r.created_at)[:16] if r.created_at else '' "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): def fmt_size(sz):
if sz < 1024: return str(sz) + "B" if sz < 1024: return str(sz) + "B"

View File

@ -3,18 +3,21 @@ id = ns.get('id')
kb_id = ns.get('kb_id', '') kb_id = ns.get('kb_id', '')
db = DBPools() db = DBPools()
dbname = get_module_dbname('rag') dbname = get_module_dbname('rag')
async with db.sqlorContext(dbname) as sor: try:
if not id: async with db.sqlorContext(dbname) as sor:
recs = await sor.sqlExe( if not id:
"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", recs = await sor.sqlExe(
{"kb_id": kb_id}) "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",
result = [{"id": "__root__", "parentid": "", "label": "📁 根目录"}] {"kb_id": kb_id})
for r in recs: result = [{"id": "__root__", "parentid": "", "label": "📁 根目录"}]
result.append({"id": r.id, "parentid": r.parentid, "label": r.label}) for r in recs:
return result result.append({"id": r.id, "parentid": r.parentid, "label": r.label})
else: return result
recs = await sor.sqlExe( else:
"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", recs = await sor.sqlExe(
{"kb_id": kb_id, "id": id}) "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",
return [dict(r) for r in recs] {"kb_id": kb_id, "id": id})
return [dict(r) for r in recs]
except:
return [{"id": "__root__", "parentid": "", "label": "📁 根目录(加载失败)"}]
return [] return []