Skip to content

Commit 207eb34

Browse files
authored
fix: improve error handling for knowledge base upload (AstrBotDevs#7536)
* fix: improve error handling for knowledge base upload - Log details field in KnowledgeBaseUploadError for better debugging - Distinguish between empty pre-chunked text and empty chunking result with appropriate error messages * style: format code
1 parent cc72c01 commit 207eb34

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

astrbot/core/knowledge_base/kb_helper.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,20 @@ async def upload_document(
329329
) from exc
330330

331331
if not chunks_text or not any(chunk.strip() for chunk in chunks_text):
332-
raise KnowledgeBaseUploadError(
333-
stage="chunking",
334-
user_message=("分块失败:文档内容为空,未生成任何可索引文本块。"),
335-
details={"file_name": file_name},
336-
)
332+
if pre_chunked_text is not None:
333+
raise KnowledgeBaseUploadError(
334+
stage="validation",
335+
user_message=("预分块文本为空,未提供任何可索引文本块。"),
336+
details={"file_name": file_name},
337+
)
338+
else:
339+
raise KnowledgeBaseUploadError(
340+
stage="chunking",
341+
user_message=(
342+
"分块失败:文档内容为空,未生成任何可索引文本块。"
343+
),
344+
details={"file_name": file_name},
345+
)
337346

338347
contents = []
339348
metadatas = []
@@ -423,7 +432,7 @@ async def embedding_progress_callback(current, total) -> None:
423432
return doc
424433
except Exception as e:
425434
if isinstance(e, KnowledgeBaseUploadError):
426-
logger.warning(f"上传文档失败: {e}")
435+
logger.warning(f"上传文档失败: {e}", extra={"details": e.details})
427436
else:
428437
logger.error(f"上传文档失败: {e}", exc_info=True)
429438
# if file_path.exists():

0 commit comments

Comments
 (0)