fix: 异常信息提炼

This commit is contained in:
2026-07-27 17:55:58 +08:00
parent 7d654ec06a
commit f5c1c16062
5 changed files with 35 additions and 24 deletions

View File

@@ -17,5 +17,13 @@ public interface ErrorCodeConstants {
ErrorCode TRANSPORT_TASK_OPERATION_NOT_SUPPORTED = new ErrorCode(5006, "不支持的任务操作类型");
ErrorCode TRANSPORT_TASK_ALREADY_FINAL = new ErrorCode(5007, "任务已处于终态,不允许操作");
ErrorCode TRANSPORT_TASK_AUTO_ISSUE_NOT_ALLOW_MANUAL = new ErrorCode(5008, "自动任务不允许手动下发");
ErrorCode TRANSPORT_TASK_OPERATING = new ErrorCode(5009, "任务标识为:{}的任务正在操作中!");
ErrorCode TRANSPORT_TASK_ISSUE_FAILED = new ErrorCode(5010, "任务下发失败,失败原因:{}");
ErrorCode TRANSPORT_TASK_ACS_SERVER_ADDRESS_NOT_CONFIGURED = new ErrorCode(5011, "未配置 ACS 服务地址:{}");
ErrorCode TRANSPORT_TASK_ACS_OPERATE_CHECK_FAILED = new ErrorCode(5012, "ACS操作校验失败");
ErrorCode TRANSPORT_TASK_PRODUCT_AREA_EMPTY = new ErrorCode(5013, "{}生产区域为空,无法获取 ACS 服务地址");
ErrorCode TRANSPORT_TASK_ACS_OPERATE_CHECK_RESULT_EMPTY = new ErrorCode(5014, "ACS 操作校验返回为空");
ErrorCode TRANSPORT_TASK_ACS_OPERATE_NOT_ALLOW = new ErrorCode(5015, "ACS 不允许执行该操作,原因:{}");
ErrorCode TRANSPORT_TASK_OPERATION_FAILED = new ErrorCode(5016, "任务操作失败,失败原因:{}");
}

View File

@@ -1,6 +1,5 @@
package cn.code.nl.module.task.manage;
import cn.code.nl.framework.common.exception.ServiceException;
import cn.code.nl.framework.common.pojo.CommonResult;
import cn.code.nl.framework.execute.biz.api.TaskCommonApi;
import cn.code.nl.framework.execute.biz.dto.TaskStatusCallApiReqDTO;
@@ -22,6 +21,8 @@ import java.util.EnumMap;
import java.util.Map;
import java.util.function.BiFunction;
import static cn.code.nl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_OPERATING;
import static cn.code.nl.module.task.framework.common.util.TaskUtil.isAllowedFrom;
/**
@@ -81,7 +82,7 @@ public class TransportTaskBusinessOperationManager {
}
}
} else {
throw new ServiceException(5007, "任务标识为:" + task.getTaskId() + "的任务正在操作中!");
throw exception(TRANSPORT_TASK_OPERATING, task.getTaskId());
}
}

View File

@@ -24,7 +24,11 @@ import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import static cn.code.nl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.code.nl.module.task.enums.AcsApiConstants.ACS_TASK_API;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_ACS_SERVER_ADDRESS_NOT_CONFIGURED;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_ISSUE_FAILED;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_PRODUCT_AREA_EMPTY;
/**
* 搬运任务下发管理器
@@ -64,7 +68,7 @@ public class TransportTaskIssueManager {
*/
public void issueTask(TransportTaskDO task) {
if (StrUtil.isBlank(task.getProductArea())) {
throw new ServiceException(500, buildIssueFailedMessage("生产区域为空,无法获取 ACS 服务地址"));
throw exception(TRANSPORT_TASK_PRODUCT_AREA_EMPTY, "任务下发失败,失败原因:");
}
List<AcsTaskDTO> acsTasks = List.of(AcsTaskUtil.buildAcsTaskDTO(task));
String requestJson = JsonUtils.toJsonString(acsTasks);
@@ -76,7 +80,7 @@ public class TransportTaskIssueManager {
throw ex;
} catch (Exception ex) {
log.error("下发单个任务失败productArea={}tasks={}", task.getProductArea(), requestJson, ex);
throw new ServiceException(500, buildIssueFailedMessage(ex.getMessage()));
throw exception(TRANSPORT_TASK_ISSUE_FAILED, ex.getMessage());
}
}
@@ -110,7 +114,7 @@ public class TransportTaskIssueManager {
CommonResult<String> result = configApi.getConfigValueByKey(configKey);
String serverAddress = result.getCheckedData();
if (StrUtil.isBlank(serverAddress)) {
throw new ServiceException(500, "未配置 ACS 服务地址:" + configKey);
throw exception(TRANSPORT_TASK_ACS_SERVER_ADDRESS_NOT_CONFIGURED, configKey);
}
return serverAddress;
}
@@ -156,7 +160,7 @@ public class TransportTaskIssueManager {
*/
private void handleSingleIssueResult(TransportTaskDO task, AcsIssueResultRespDTO result) {
if (result == null) {
throw new ServiceException(500, buildIssueFailedMessage("ACS 返回为空"));
throw exception(TRANSPORT_TASK_ISSUE_FAILED, "ACS 返回为空");
}
String resultJson = JsonUtils.toJsonString(result);
if (Boolean.TRUE.equals(result.getSuccess())) {
@@ -166,17 +170,7 @@ public class TransportTaskIssueManager {
String errorMessage = CollUtil.isNotEmpty(result.getFailedTasks())
? result.getFailedTasks().get(0).getErrorMessage()
: result.getMsg();
throw new ServiceException(500, buildIssueFailedMessage(StrUtil.blankToDefault(errorMessage, "ACS 下发失败")));
}
/**
* 构建单个任务下发失败提示
*
* @param reason 失败原因
* @return 失败提示
*/
private String buildIssueFailedMessage(String reason) {
return "任务下发失败,失败原因:" + reason;
throw exception(TRANSPORT_TASK_ISSUE_FAILED, StrUtil.blankToDefault(errorMessage, "ACS 下发失败"));
}
/**

View File

@@ -14,7 +14,13 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import static cn.code.nl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.code.nl.module.task.enums.AcsApiConstants.ACS_OPERATE_CHECK_API;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_ACS_OPERATE_CHECK_FAILED;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_ACS_OPERATE_CHECK_RESULT_EMPTY;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_ACS_OPERATE_NOT_ALLOW;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_ACS_SERVER_ADDRESS_NOT_CONFIGURED;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_PRODUCT_AREA_EMPTY;
/**
* PC 端任务操作 ACS 校验管理器
@@ -50,7 +56,7 @@ public class TransportTaskOperateCheckManager {
throw ex;
} catch (Exception ex) {
log.error("ACS 操作校验请求失败reqDTO={}", JsonUtils.toJsonString(reqDTO), ex);
throw new ServiceException(500, "ACS操作校验失败");
throw exception(TRANSPORT_TASK_ACS_OPERATE_CHECK_FAILED);
}
handleCheckResult(reqDTO, result);
}
@@ -71,13 +77,13 @@ public class TransportTaskOperateCheckManager {
*/
private String getAcsServerAddress(String productArea) {
if (StrUtil.isBlank(productArea)) {
throw new ServiceException(500, "生产区域为空,无法获取 ACS 服务地址");
throw exception(TRANSPORT_TASK_PRODUCT_AREA_EMPTY, "");
}
String configKey = productArea + ACS_SERVER_ADDRESS_CONFIG_SUFFIX;
CommonResult<String> result = configApi.getConfigValueByKey(configKey);
String serverAddress = result.getCheckedData();
if (StrUtil.isBlank(serverAddress)) {
throw new ServiceException(500, "未配置 ACS 服务地址:" + configKey);
throw exception(TRANSPORT_TASK_ACS_SERVER_ADDRESS_NOT_CONFIGURED, configKey);
}
return serverAddress;
}
@@ -99,7 +105,7 @@ public class TransportTaskOperateCheckManager {
*/
private void handleCheckResult(AcsOperateCheckReqDTO reqDTO, AcsOperateCheckRespDTO result) {
if (result == null) {
throw new ServiceException(500, "ACS 操作校验返回为空");
throw exception(TRANSPORT_TASK_ACS_OPERATE_CHECK_RESULT_EMPTY);
}
Boolean enableOperate = getEnableOperate(result);
if (Boolean.TRUE.equals(enableOperate)) {
@@ -108,7 +114,7 @@ public class TransportTaskOperateCheckManager {
String message = getMessage(result);
log.warn("ACS 拒绝 PC 端任务操作reqDTO={}result={}",
JsonUtils.toJsonString(reqDTO), JsonUtils.toJsonString(result));
throw new ServiceException(500, message);
throw exception(TRANSPORT_TASK_ACS_OPERATE_NOT_ALLOW, message);
}
/**

View File

@@ -28,6 +28,8 @@ import java.util.function.BiConsumer;
import static cn.code.nl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_AUTO_ISSUE_NOT_ALLOW_MANUAL;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_OPERATING;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_OPERATION_FAILED;
import static cn.code.nl.module.task.enums.ErrorCodeConstants.TRANSPORT_TASK_STATUS_NOT_ALLOW;
import static cn.code.nl.module.task.framework.common.util.TaskUtil.isAllowedFrom;
@@ -86,14 +88,14 @@ public class TransportTaskOperationManager {
throw serviceException;
}
log.error("[messageResend][执行异常][lockKey={}]", task.getTaskId(), ex);
throw new ServiceException(500, ex.getMessage());
throw exception(TRANSPORT_TASK_OPERATION_FAILED, ex.getMessage());
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
} else {
throw new ServiceException(5007, "任务标识为:" + task.getTaskId() + "的任务正在操作中!");
throw exception(TRANSPORT_TASK_OPERATING, task.getTaskId());
}
}