From b645f86822ce6d598b641044f315ee52b0f0b776 Mon Sep 17 00:00:00 2001 From: Liuyang <2746366019@qq.com> Date: Wed, 26 Feb 2025 13:49:41 +0800 Subject: [PATCH] =?UTF-8?q?feat(system):=20=E6=B7=BB=E5=8A=A0=20HTTP=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E9=94=99=E8=AF=AF=E7=A0=81=E5=B9=B6=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 ErrorCodeConstants 中新增 HTTP 请求相关的错误码 - 在 FineTuningTaskHttpService 中添加异常捕获 --- .../http/FineTuningTaskHttpService.java | 36 ++++++++++++++++--- .../system/enums/ErrorCodeConstants.java | 4 +++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/FineTuningTaskHttpService.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/FineTuningTaskHttpService.java index 6707a31f4..f99448564 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/FineTuningTaskHttpService.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/http/FineTuningTaskHttpService.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.llm.service.http; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; +import cn.iocoder.yudao.framework.common.exception.ErrorCode; import cn.iocoder.yudao.framework.common.util.http.HttpUtils; import cn.iocoder.yudao.module.llm.dal.dataobject.finetuningtask.FineTuningTaskDO; import cn.iocoder.yudao.module.llm.framework.backend.config.LLMBackendProperties; @@ -16,10 +17,15 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.IOException; +import java.net.ConnectException; +import java.net.SocketTimeoutException; import java.util.Map; import java.util.Objects; import java.util.concurrent.TimeUnit; +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; + /** * @Description 微调任务相关接口 */ @@ -88,16 +94,16 @@ public class FineTuningTaskHttpService { // TODO: 在上个方法中已经将数据集的文件id赋予,调试时需要写死再放开 // String fileId = "6237ed4d-a046-479c-80d6-8579a0283994"; // req.setFileId(fileId); - + String requestUrl = url + llmBackendProperties.getFinetuningCreate(); try { // 记录请求信息 - log.info("开始创建微调任务,请求URL: {}", url + llmBackendProperties.getFinetuningCreate()); + log.info("开始创建微调任务,请求URL: {}", requestUrl); log.info("请求头: {}", headers); log.info("请求体: {}", JSON.toJSONString(req)); // 发起 HTTP 请求 log.debug("正在发起 HTTP POST 请求..."); - String res = HttpUtils.post(url + llmBackendProperties.getFinetuningCreate(), headers, JSON.toJSONString(req)); + String res = HttpUtils.post(requestUrl, headers, JSON.toJSONString(req)); log.info("HTTP 请求完成。响应内容: {}", res); // 解析响应 @@ -110,8 +116,28 @@ public class FineTuningTaskHttpService { return aigcFineTuningCreateRespVO; } catch (Exception e) { - log.error("创建微调任务时发生异常。请求URL: {}", url + llmBackendProperties.getFinetuningCreate(), e); - throw new RuntimeException("微调任务创建失败", e); + log.error("创建微调任务时发生异常。请求URL: {}", requestUrl, e); + handleHttpException(e); + } + return null; + } + + /** + * 统一处理 HTTP 请求异常 + */ + private void handleHttpException(Exception e) { + if (e instanceof ConnectException) { + log.error("连接后端服务失败,请检查后端服务是否正常。"); + throw exception(HTTP_CONNECTION_REFUSED); + } else if (e instanceof SocketTimeoutException) { + log.error("连接后端服务超时,请检查网络或后端服务是否正常。"); + throw exception(HTTP_CONNECTION_TIMEOUT); + } else if (e instanceof IOException) { + log.error("HTTP 请求发生 IO 异常: {}", e.getMessage()); + throw exception(HTTP_IO_ERROR); + } else { + log.error("未知异常: {}", e.getMessage()); + throw new RuntimeException("HTTP 请求发生未知异常", e); } } } diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java index 96052dc72..74861f344 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java @@ -163,4 +163,8 @@ public interface ErrorCodeConstants { // ========== 站内信发送 1-002-028-000 ========== ErrorCode NOTIFY_SEND_TEMPLATE_PARAM_MISS = new ErrorCode(1_002_028_000, "模板参数({})缺失"); + // ========== HTTP 模块错误码 1-003-000-000========== + ErrorCode HTTP_CONNECTION_REFUSED = new ErrorCode(1_003_000_000, "HTTP 请求失败,连接被拒绝"); + ErrorCode HTTP_CONNECTION_TIMEOUT = new ErrorCode(1_003_000_001, "HTTP 请求失败,连接超时"); + ErrorCode HTTP_IO_ERROR = new ErrorCode(1_003_000_002, "HTTP 请求失败,IO 异常"); }