From 96da84dad4adb8186eecf6f86a9fbe6483936bfe Mon Sep 17 00:00:00 2001 From: rainv123 <2148537152@qq.com> Date: Tue, 11 Nov 2025 16:02:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E9=94=99=E8=AF=AF=E5=A4=84=E7=90=86?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xiaozhi/common/exception/ErrorCode.java | 2 +- .../impl/KnowledgeFilesServiceImpl.java | 295 +++++++----------- .../main/resources/i18n/messages.properties | 2 +- .../resources/i18n/messages_en_US.properties | 2 +- .../resources/i18n/messages_zh_CN.properties | 2 +- .../resources/i18n/messages_zh_TW.properties | 2 +- .../src/views/KnowledgeFileUpload.vue | 49 ++- 7 files changed, 167 insertions(+), 187 deletions(-) diff --git a/main/manager-api/src/main/java/xiaozhi/common/exception/ErrorCode.java b/main/manager-api/src/main/java/xiaozhi/common/exception/ErrorCode.java index b8525e63..268df223 100644 --- a/main/manager-api/src/main/java/xiaozhi/common/exception/ErrorCode.java +++ b/main/manager-api/src/main/java/xiaozhi/common/exception/ErrorCode.java @@ -205,7 +205,7 @@ public interface ErrorCode { int RAG_CONFIG_NOT_FOUND = 10164; // RAG配置未找到 int RAG_CONFIG_TYPE_ERROR = 10165; // RAG配置类型错误 int RAG_DEFAULT_CONFIG_NOT_FOUND = 10166; // 默认RAG配置未找到 - int RAG_API_ERROR = 10167; // RAG配置缺少必要参数 + int RAG_API_ERROR = 10167; // RAG调用失败 int UPLOAD_FILE_ERROR = 10168; // 上传文件失败 int NO_PERMISSION = 10169; // 没有权限 int KNOWLEDGE_BASE_NAME_EXISTS = 10170; // 同名知识库已存在 diff --git a/main/manager-api/src/main/java/xiaozhi/modules/knowledge/service/impl/KnowledgeFilesServiceImpl.java b/main/manager-api/src/main/java/xiaozhi/modules/knowledge/service/impl/KnowledgeFilesServiceImpl.java index 458ef063..3608399b 100644 --- a/main/manager-api/src/main/java/xiaozhi/modules/knowledge/service/impl/KnowledgeFilesServiceImpl.java +++ b/main/manager-api/src/main/java/xiaozhi/modules/knowledge/service/impl/KnowledgeFilesServiceImpl.java @@ -16,15 +16,11 @@ import org.springframework.core.io.AbstractResource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.HttpServerErrorException; -import org.springframework.web.client.ResourceAccessException; import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; @@ -112,8 +108,8 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); if (!response.getStatusCode().is2xxSuccessful()) { - log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); - throw new RenException(ErrorCode.RAG_API_ERROR); + log.error("RAGFlow API调用失败,状态码: {}", response.getStatusCode()); + throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败,HTTP状态码: " + response.getStatusCode()); } String responseBody = response.getBody(); @@ -127,27 +123,20 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { Object dataObj = responseMap.get("data"); return parseDocumentListResponse(dataObj, page, limit); } else { - log.error("RAGFlow API调用失败,响应码: {}, 响应内容: {}", code, responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败,响应码: " + code); + log.error("RAGFlow API调用失败,响应码: {}", code); + // 获取错误消息,如果存在的话 + String apiMessage = (String) responseMap.get("message"); + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } - } catch (IOException e) { - log.error("解析RAGFlow API响应失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析RAGFlow响应失败: " + e.getMessage()); - } catch (HttpClientErrorException e) { - log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档列表失败: " + e.getMessage()); - } catch (HttpServerErrorException e) { - log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档列表失败: " + e.getMessage()); - } catch (ResourceAccessException e) { - log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档列表失败: 网络连接错误 - " + e.getMessage()); } catch (Exception e) { log.error("获取文档列表失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取文档列表失败: " + e.getMessage()); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + if (e instanceof RenException) { + throw (RenException) e; + } + + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } finally { log.info("=== 获取文档列表操作结束 ==="); } @@ -480,8 +469,11 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); if (!response.getStatusCode().is2xxSuccessful()) { - log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); - throw new RenException(ErrorCode.RAG_API_ERROR); + log.error("RAGFlow API调用失败,状态码: {}", response.getStatusCode()); + String responseBody = response.getBody(); + throw new RenException(ErrorCode.RAG_API_ERROR, + "RAGFlow API调用失败,HTTP状态码: " + response.getStatusCode() + + ", 响应内容: " + (responseBody != null ? responseBody : "无响应内容")); } String responseBody = response.getBody(); @@ -502,32 +494,20 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { } throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS); } else { - log.error("RAGFlow API调用失败,响应码: {}, 响应内容: {}", code, responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败,响应码: " + code); + log.error("RAGFlow API调用失败,响应码: {}", code); + // 获取错误消息,如果存在的话 + String apiMessage = (String) responseMap.get("message"); + String errorDetail = apiMessage != null ? apiMessage : "无详细错误信息"; + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } - } catch (IOException e) { - log.error("解析RAGFlow API响应失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, - "解析RAGFlow响应失败: " + e.getMessage()); - } catch (HttpClientErrorException e) { - if (e.getStatusCode() == HttpStatus.NOT_FOUND) { - log.warn("文档不存在,documentId: {}, datasetId: {}", documentId, datasetId); - throw new RenException(ErrorCode.Knowledge_Base_RECORD_NOT_EXISTS); - } - log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString()); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档失败: " + e.getMessage()); - } catch (HttpServerErrorException e) { - log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString()); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档失败: " + e.getMessage()); - } catch (ResourceAccessException e) { - log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档失败: 网络连接错误 - " + e.getMessage()); } catch (Exception e) { log.error("根据documentId获取文档失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取文档失败: " + e.getMessage()); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + if (e instanceof RenException) { + throw (RenException) e; + } + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } finally { log.info("=== 根据documentId获取文档操作结束 ==="); } @@ -583,8 +563,8 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); if (!response.getStatusCode().is2xxSuccessful()) { - log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); - throw new RenException(ErrorCode.RAG_API_ERROR); + log.error("RAGFlow API调用失败,状态码: {}", response.getStatusCode()); + throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败,HTTP状态码: " + response.getStatusCode()); } String responseBody = response.getBody(); @@ -615,25 +595,10 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { datasetId, status, pageData.getList().size()); return pageData; } else { - log.error("RAGFlow API调用失败,响应码: {}, 响应内容: {}", code, responseBody); + log.error("RAGFlow API调用失败,响应码: {}", code); throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败,响应码: " + code); } - } catch (IOException e) { - log.error("解析RAGFlow API响应失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, - "解析RAGFlow响应失败: " + e.getMessage()); - } catch (HttpClientErrorException e) { - log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString()); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档列表失败: " + e.getMessage()); - } catch (HttpServerErrorException e) { - log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString()); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档列表失败: " + e.getMessage()); - } catch (ResourceAccessException e) { - log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "获取RAGFlow文档列表失败: 网络连接错误 - " + e.getMessage()); } catch (Exception e) { log.error("根据状态查询文档列表失败: {}", e.getMessage(), e); throw new RenException(ErrorCode.RAG_API_ERROR, "查询文档列表失败: " + e.getMessage()); @@ -722,7 +687,11 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { } catch (Exception e) { log.error("删除文档失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "删除文档失败: " + e.getMessage()); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + if (e instanceof RenException) { + throw (RenException) e; + } + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } finally { log.info("=== 根据documentId删除文档操作结束 ==="); } @@ -842,7 +811,7 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { // 验证base_url是否存在且非空 if (StringUtils.isBlank(baseUrl)) { - throw new RenException(ErrorCode.RAG_API_ERROR); + throw new RenException(ErrorCode.RAG_API_ERROR, "RAG配置缺少必要参数: base_url"); } } @@ -939,44 +908,40 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("文档上传成功,documentId: {}", documentId); } else { // 如果响应码不为0,说明API调用失败 - log.error("RAGFlow API调用失败,响应码: {}, 响应内容: {}", code, responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败,响应码: " + code); + log.error("RAGFlow API调用失败,响应码: {}", code); + // 获取错误消息,如果存在的话 + String apiMessage = (String) responseMap.get("message"); + String errorDetail = apiMessage != null ? apiMessage : "无详细错误信息"; + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } log.info("从RAGFlow API响应中解析出documentId: {}", documentId); log.debug("完整响应内容: {}", responseBody); } catch (Exception e) { - log.error("解析RAGFlow API响应失败: {}, 响应内容: {}", e.getMessage(), responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析RAGFlow响应失败: " + e.getMessage()); + log.error("解析RAGFlow API响应失败: {}", e.getMessage()); + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } } else { - log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR); + log.error("RAGFlow API调用失败,状态码: {}", response.getStatusCode()); + throw new RenException(ErrorCode.RAG_API_ERROR, + "RAGFlow API调用失败,HTTP状态码: " + response.getStatusCode() + + ", 响应内容: " + (responseBody != null ? responseBody : "无响应内容")); } if (StringUtils.isBlank(documentId)) { - log.error("无法从RAGFlow API响应中获取documentId,响应内容: {}", responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API响应中未包含documentId"); + log.error("无法从RAGFlow API响应中获取documentId"); + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } log.info("RAGFlow文档上传成功,documentId: {},文档已开始自动解析切片", documentId); return documentId; - } catch (HttpClientErrorException e) { - log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, - "上传RAGFlow文档失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); - } catch (HttpServerErrorException e) { - log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, - "上传RAGFlow文档失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); - } catch (ResourceAccessException e) { - log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "上传RAGFlow文档失败: 网络连接错误 - " + e.getMessage()); } catch (Exception e) { - log.error("RAGFlow API调用失败 - 未知错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "上传RAGFlow文档失败: " + e.getMessage()); + log.error("RAGFlow API调用失败: {}", e.getMessage(), e); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + if (e instanceof RenException) { + throw (RenException) e; + } + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } } @@ -1093,8 +1058,9 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { return; } else { String message = (String) responseMap.get("message"); - log.error("RAGFlow API调用失败,响应码: {}, 消息: {}", code, message); - throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败: " + message); + log.error("RAGFlow API调用失败,响应码: {}", code); + String errorDetail = message != null ? message : "无详细错误信息"; + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } } catch (Exception e) { log.warn("解析RAGFlow响应失败,但HTTP状态码成功,视为删除成功: {}", e.getMessage()); @@ -1106,26 +1072,19 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { return; } } else { - log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR); + log.error("RAGFlow API调用失败,状态码: {}", response.getStatusCode()); + throw new RenException(ErrorCode.RAG_API_ERROR, + "RAGFlow API调用失败,HTTP状态码: " + response.getStatusCode() + + ", 响应内容: " + (responseBody != null ? responseBody : "无响应内容")); } - } catch (HttpClientErrorException e) { - log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, - "删除RAGFlow文档失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); - } catch (HttpServerErrorException e) { - log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, - "删除RAGFlow文档失败: " + e.getMessage() + ", 响应: " + e.getResponseBodyAsString()); - } catch (ResourceAccessException e) { - log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "删除RAGFlow文档失败: 网络连接错误 - " + e.getMessage()); } catch (Exception e) { - log.error("RAGFlow API调用失败 - 未知错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "删除RAGFlow文档失败: " + e.getMessage()); + log.error("RAGFlow API调用失败: {}", e.getMessage(), e); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + if (e instanceof RenException) { + throw (RenException) e; + } + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } } @@ -1204,14 +1163,16 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); - if (!response.getStatusCode().is2xxSuccessful()) { - log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); - throw new RenException(ErrorCode.RAG_API_ERROR); - } - String responseBody = response.getBody(); log.debug("RAGFlow API响应内容: {}", responseBody); + if (!response.getStatusCode().is2xxSuccessful()) { + log.error("RAGFlow API调用失败,状态码: {}", response.getStatusCode()); + String errorDetail = responseBody != null ? responseBody : "无响应内容"; + throw new RenException(ErrorCode.RAG_API_ERROR, + "RAGFlow API调用失败,HTTP状态码: " + response.getStatusCode() + ", 响应内容: " + errorDetail); + } + // 解析响应 Map responseMap = objectMapper.readValue(responseBody, Map.class); Integer code = (Integer) responseMap.get("code"); @@ -1220,28 +1181,20 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("文档解析成功,datasetId: {}, documentIds: {}", datasetId, documentIds); return true; } else { + // 获取错误消息,如果存在的话 String message = (String) responseMap.get("message"); - log.error("RAGFlow API调用失败,响应码: {}, 消息: {}, 响应内容: {}", code, message, responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR, response.getStatusCode().toString()); + String errorDetail = message != null ? message : "无详细错误信息"; + log.error("RAGFlow API调用失败,响应码: {}, 错误信息: {}", code, errorDetail); + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } - } catch (IOException e) { - log.error("解析RAGFlow API响应失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析RAGFlow响应失败: " + e.getMessage()); - } catch (HttpClientErrorException e) { - log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析RAGFlow文档失败: " + e.getMessage()); - } catch (HttpServerErrorException e) { - log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析RAGFlow文档失败: " + e.getMessage()); - } catch (ResourceAccessException e) { - log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析RAGFlow文档失败: 网络连接错误 - " + e.getMessage()); } catch (Exception e) { log.error("解析文档失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析文档失败: " + e.getMessage()); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + if (e instanceof RenException) { + throw (RenException) e; + } + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } finally { log.info("=== 解析文档操作结束 ==="); } @@ -1308,14 +1261,16 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); - if (!response.getStatusCode().is2xxSuccessful()) { - log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); - throw new RenException(ErrorCode.RAG_API_ERROR, "列出切片失败"); - } - String responseBody = response.getBody(); log.debug("RAGFlow API响应内容: {}", responseBody); + if (!response.getStatusCode().is2xxSuccessful()) { + log.error("RAGFlow API调用失败,状态码: {}", response.getStatusCode()); + String errorDetail = responseBody != null ? responseBody : "无响应内容"; + throw new RenException(ErrorCode.RAG_API_ERROR, + "RAGFlow API调用失败,HTTP状态码: " + response.getStatusCode() + ", 响应内容: " + errorDetail); + } + // 解析响应 Map responseMap = objectMapper.readValue(responseBody, Map.class); Integer code = (Integer) responseMap.get("code"); @@ -1326,28 +1281,24 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { // 解析切片数据并格式化返回 return parseChunkListResponse(responseMap); } else { + // 获取错误消息,如果存在的话 String message = (String) responseMap.get("message"); - log.error("RAGFlow API调用失败,响应码: {}, 消息: {}, 响应内容: {}", code, message, responseBody); - throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败: " + message); + String errorDetail = message != null ? message : "无详细错误信息"; + log.error("RAGFlow API调用失败,响应码: {}, 错误信息: {}", code, errorDetail); + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } } catch (IOException e) { log.error("解析RAGFlow API响应失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析RAGFlow响应失败: " + e.getMessage()); - } catch (HttpClientErrorException e) { - log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "列出切片失败: " + e.getMessage()); - } catch (HttpServerErrorException e) { - log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "列出切片失败: " + e.getMessage()); - } catch (ResourceAccessException e) { - log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "列出切片失败: 网络连接错误 - " + e.getMessage()); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } catch (Exception e) { log.error("列出切片失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "列出切片失败: " + e.getMessage()); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + if (e instanceof RenException) { + throw (RenException) e; + } + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } finally { log.info("=== 列出切片操作结束 ==="); } @@ -1736,14 +1687,16 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("RAGFlow API响应状态码: {}", response.getStatusCode()); - if (!response.getStatusCode().is2xxSuccessful()) { - log.error("RAGFlow API调用失败,状态码: {}, 响应内容: {}", response.getStatusCode(), response.getBody()); - throw new RenException(ErrorCode.RAG_API_ERROR); - } - String responseBody = response.getBody(); log.debug("RAGFlow API响应内容: {}", responseBody); + if (!response.getStatusCode().is2xxSuccessful()) { + log.error("RAGFlow API调用失败,状态码: {}", response.getStatusCode()); + String errorDetail = responseBody != null ? responseBody : "无响应内容"; + throw new RenException(ErrorCode.RAG_API_ERROR, + "RAGFlow API调用失败,HTTP状态码: " + response.getStatusCode() + ", 响应内容: " + errorDetail); + } + // 解析响应 Map responseMap = objectMapper.readValue(responseBody, Map.class); Integer code = (Integer) responseMap.get("code"); @@ -1755,32 +1708,24 @@ public class KnowledgeFilesServiceImpl implements KnowledgeFilesService { log.info("召回测试成功,返回 {} 条切片", result.get("total")); return result; } else { - log.error("RAGFlow API响应格式错误,data字段不是Map类型: {}", dataObj); - throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API响应格式错误"); + log.error("RAGFlow API响应格式错误,data字段不是Map类型"); + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } } else { + // 获取错误消息,如果存在的话 String message = (String) responseMap.get("message"); - log.error("RAGFlow API调用失败,响应码: {}, 错误信息: {}", code, message); - throw new RenException(ErrorCode.RAG_API_ERROR, "RAGFlow API调用失败: " + message); + String errorDetail = message != null ? message : "无详细错误信息"; + log.error("RAGFlow API调用失败,响应码: {}, 错误信息: {}", code, errorDetail); + throw new RenException(ErrorCode.RAG_API_ERROR, responseBody); } - } catch (IOException e) { - log.error("解析RAGFlow API响应失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "解析RAGFlow响应失败: " + e.getMessage()); - } catch (HttpClientErrorException e) { - log.error("RAGFlow API调用失败 - HTTP错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "召回测试失败: " + e.getMessage()); - } catch (HttpServerErrorException e) { - log.error("RAGFlow API调用失败 - 服务器错误: {}, 状态码: {}, 响应内容: {}", - e.getMessage(), e.getStatusCode(), e.getResponseBodyAsString(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "召回测试失败: " + e.getMessage()); - } catch (ResourceAccessException e) { - log.error("RAGFlow API调用失败 - 网络连接错误: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "召回测试失败: 网络连接错误 - " + e.getMessage()); } catch (Exception e) { log.error("召回测试失败: {}", e.getMessage(), e); - throw new RenException(ErrorCode.RAG_API_ERROR, "召回测试失败: " + e.getMessage()); + String errorMessage = e.getMessage() != null ? e.getMessage() : "未知错误"; + if (e instanceof RenException) { + throw (RenException) e; + } + throw new RenException(ErrorCode.RAG_API_ERROR, errorMessage); } finally { log.info("=== 召回测试操作结束 ==="); } diff --git a/main/manager-api/src/main/resources/i18n/messages.properties b/main/manager-api/src/main/resources/i18n/messages.properties index f927e7f7..ebde5cdf 100644 --- a/main/manager-api/src/main/resources/i18n/messages.properties +++ b/main/manager-api/src/main/resources/i18n/messages.properties @@ -173,7 +173,7 @@ 10164=RAG\u914D\u7F6E\u672A\u627E\u5230 10165=RAG\u914D\u7F6E\u7C7B\u578B\u9519\u8BEF 10166=\u9ED8\u8BA4RAG\u914D\u7F6E\u672A\u627E\u5230 -10167=RAG\u914d\u7f6e\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570\uFF0C{0} +10167=\u0052\u0041\u0047\u8c03\u7528\u5931\u8d25\uFF0C{0} 10168=\u4E0A\u4F20\u6587\u4EF6\u5931\u8D25 10169=\u60A8\u6CA1\u6709\u6743\u9650\u64CD\u4F5C\u8BE5\u8BB0\u5F55 10170=\u77E5\u8BC6\u5E93\u540D\u79F0\u91CD\u590D \ No newline at end of file diff --git a/main/manager-api/src/main/resources/i18n/messages_en_US.properties b/main/manager-api/src/main/resources/i18n/messages_en_US.properties index 2d318dbc..74b29caa 100644 --- a/main/manager-api/src/main/resources/i18n/messages_en_US.properties +++ b/main/manager-api/src/main/resources/i18n/messages_en_US.properties @@ -173,7 +173,7 @@ 10164=RAG configuration not found 10165=RAG configuration type error 10166=Default RAG configuration not found -10167=RAG configuration missing necessary parameters: {0} +10167=RAG API call failed: {0} 10168=Upload file failed 10169=No permission to operate this knowledge base 10170=Knowledge base name already exists \ No newline at end of file diff --git a/main/manager-api/src/main/resources/i18n/messages_zh_CN.properties b/main/manager-api/src/main/resources/i18n/messages_zh_CN.properties index 1a9a2a63..e319c922 100644 --- a/main/manager-api/src/main/resources/i18n/messages_zh_CN.properties +++ b/main/manager-api/src/main/resources/i18n/messages_zh_CN.properties @@ -173,7 +173,7 @@ 10164=RAG\u914D\u7F6E\u672A\u627E\u5230 10165=RAG\u914D\u7F6E\u7C7B\u578B\u9519\u8BEF 10166=\u9ED8\u8BA4RAG\u914D\u7F6E\u672A\u627E\u5230 -10167=RAG\u914d\u7f6e\u7f3a\u5c11\u5fc5\u8981\u53c2\u6570\uFF0C{0} +10167=\u0052\u0041\u0047\u8c03\u7528\u5931\u8d25\uFF0C{0} 10168=\u4E0A\u4F20\u6587\u4EF6\u5931\u8D25 10169=\u60A8\u6CA1\u6709\u6743\u9650\u64CD\u4F5C\u8BE5\u8BB0\u5F55 10170=\u77E5\u8BC6\u5E93\u540D\u79F0\u91CD\u590D \ No newline at end of file diff --git a/main/manager-api/src/main/resources/i18n/messages_zh_TW.properties b/main/manager-api/src/main/resources/i18n/messages_zh_TW.properties index 5b9465ec..026664f6 100644 --- a/main/manager-api/src/main/resources/i18n/messages_zh_TW.properties +++ b/main/manager-api/src/main/resources/i18n/messages_zh_TW.properties @@ -173,7 +173,7 @@ 10164=RAG\u914D\u7F6E\u672A\u627E\u5230 10165=RAG\u914D\u7F6E\u985E\u578B\u932F\u8AA4 10166=\u9810\u8A2DRAG\u914D\u7F6E\u672A\u627E\u5230 -10167=RAG\u914d\u7f6e\u6f0f\u5c11\u5fc5\u8981\u53c2\u6570\uFF0C{0} +10167=\u0052\u0041\u0047\u8abf\u7528\u5931\u6557\uFF0C{0} 10168=\u4E0A\u50B3\u6587\u4EF6\u5931\u6557 10169=\u60A8\u6C92\u6709\u6B0A\u9650\u64CD\u4F5C\u8A72\u8A18\u9304 10170=\u77E5\u8B58\u5EAB\u540D\u7A31\u91CD\u8907 diff --git a/main/manager-web/src/views/KnowledgeFileUpload.vue b/main/manager-web/src/views/KnowledgeFileUpload.vue index 134f7fd9..dec0da85 100644 --- a/main/manager-web/src/views/KnowledgeFileUpload.vue +++ b/main/manager-web/src/views/KnowledgeFileUpload.vue @@ -663,7 +663,12 @@ export default { } }, (err) => { - reject({ success: false, fileName: file.name, error: this.$t('knowledgeFileUpload.uploadFailed') }); + // 错误回调处理后端返回的错误信息 + if (err && err.data) { + reject({ success: false, fileName: file.name, error: err.data.msg || err.msg || this.$t('knowledgeFileUpload.uploadFailed') }); + } else { + reject({ success: false, fileName: file.name, error: this.$t('knowledgeFileUpload.uploadFailed') }); + } console.error('上传文档失败:', err); } ); @@ -724,7 +729,12 @@ export default { }, (err) => { this.uploading = false; - this.$message.error(this.$t('knowledgeFileUpload.uploadFailed')); + // 错误回调处理后端返回的错误信息 + if (err && err.data) { + this.$message.error(err.data.msg || err.msg || this.$t('knowledgeFileUpload.uploadFailed')); + } else { + this.$message.error(this.$t('knowledgeFileUpload.uploadFailed')); + } console.error('上传文档失败:', err); } ); @@ -757,7 +767,12 @@ export default { } }, (err) => { - this.$message.error(this.$t('knowledgeFileUpload.parseFailed')); + // 错误回调处理后端返回的错误信息 + if (err && err.data) { + this.$message.error(err.data.msg || err.msg || this.$t('knowledgeFileUpload.parseFailed')); + } else { + this.$message.error(this.$t('knowledgeFileUpload.parseFailed')); + } console.error('解析文档失败:', err); } ); @@ -790,7 +805,12 @@ export default { } }, (err) => { - this.$message.error(this.$t('knowledgeFileUpload.deleteFailed')); + // 错误回调处理后端返回的错误信息 + if (err && err.data) { + this.$message.error(err.data.msg || err.msg || this.$t('knowledgeFileUpload.deleteFailed')); + } else { + this.$message.error(this.$t('knowledgeFileUpload.deleteFailed')); + } console.error('删除文档失败:', err); } ); @@ -835,7 +855,12 @@ export default { } }, (err) => { - reject(this.$t('knowledgeFileUpload.deleteFailed')); + // 错误回调处理后端返回的错误信息 + if (err && err.data) { + reject(err.data.msg || err.msg || this.$t('knowledgeFileUpload.deleteFailed')); + } else { + reject(this.$t('knowledgeFileUpload.deleteFailed')); + } console.error('删除文档失败:', err); } ); @@ -944,7 +969,12 @@ export default { }, (err) => { this.sliceLoading = false; - this.$message.error('获取切片列表失败'); + // 错误回调处理后端返回的错误信息 + if (err && err.data) { + this.$message.error(err.data.msg || err.msg || '获取切片列表失败'); + } else { + this.$message.error('获取切片列表失败'); + } console.error('获取切片列表失败:', err); this.sliceList = []; this.sliceTotal = 0; @@ -1061,7 +1091,12 @@ export default { }, (err) => { this.retrievalTestLoading = false; - this.$message.error('召回测试失败'); + // 错误回调处理后端返回的错误信息 + if (err && err.data) { + this.$message.error(err.data.msg || err.msg || '召回测试失败'); + } else { + this.$message.error('召回测试失败'); + } console.error('召回测试失败:', err); } );