feat(system): 添加 HTTP 模块错误码并优化异常处理

- 在 ErrorCodeConstants 中新增 HTTP 请求相关的错误码
- 在 FineTuningTaskHttpService 中添加异常捕获
This commit is contained in:
Liuyang 2025-02-26 13:49:41 +08:00
parent 3a6362bab1
commit b645f86822
2 changed files with 35 additions and 5 deletions

View File

@ -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);
}
}
}

View File

@ -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 异常");
}