refactor 优化工作流服务中的异常处理

This commit is contained in:
AprilWind
2025-12-02 17:28:35 +08:00
parent e672a3bc6c
commit 1d4fcf737a
4 changed files with 173 additions and 244 deletions

View File

@@ -42,7 +42,7 @@ public class DataBaseHelper {
String databaseProductName = metaData.getDatabaseProductName(); String databaseProductName = metaData.getDatabaseProductName();
return DataBaseType.find(databaseProductName); return DataBaseType.find(databaseProductName);
} catch (SQLException e) { } catch (SQLException e) {
throw new ServiceException(e.getMessage()); throw new RuntimeException("获取数据库类型失败", e);
} }
} }

View File

@@ -211,7 +211,6 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
Function.identity() Function.identity()
); );
try {
// 逐一触发删除事件 // 逐一触发删除事件
instances.forEach(instance -> { instances.forEach(instance -> {
Definition definition = definitionMap.get(instance.getDefinitionId()); Definition definition = definitionMap.get(instance.getDefinitionId());
@@ -222,16 +221,7 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
flowProcessEventHandler.processDeleteHandler(definition.getFlowCode(), instance.getBusinessId()); flowProcessEventHandler.processDeleteHandler(definition.getFlowCode(), instance.getBusinessId());
}); });
// 删除实例 // 删除实例
boolean remove = insService.remove(instanceIds); return insService.remove(instanceIds);
if (!remove) {
log.warn("删除流程实例失败!");
throw new ServiceException("删除流程实例失败");
}
} catch (Exception e) {
log.warn("操作失败!{}", e.getMessage());
throw new ServiceException(e.getMessage());
}
return true;
} }
/** /**
@@ -254,7 +244,6 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
Definition::getId, Definition::getId,
Function.identity() Function.identity()
); );
try {
// 逐一触发删除事件 // 逐一触发删除事件
instances.forEach(instance -> { instances.forEach(instance -> {
Definition definition = definitionMap.get(instance.getDefinitionId()); Definition definition = definitionMap.get(instance.getDefinitionId());
@@ -271,10 +260,6 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
FlowEngine.taskService().deleteByInsIds(instanceIds); FlowEngine.taskService().deleteByInsIds(instanceIds);
FlowEngine.hisTaskService().deleteByInsIds(instanceIds); FlowEngine.hisTaskService().deleteByInsIds(instanceIds);
FlowEngine.insService().removeByIds(instanceIds); FlowEngine.insService().removeByIds(instanceIds);
} catch (Exception e) {
log.warn("操作失败!{}", e.getMessage());
throw new ServiceException(e.getMessage());
}
return true; return true;
} }
@@ -286,7 +271,6 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean cancelProcessApply(FlowCancelBo bo) { public boolean cancelProcessApply(FlowCancelBo bo) {
try {
Instance instance = selectInstByBusinessId(bo.getBusinessId()); Instance instance = selectInstByBusinessId(bo.getBusinessId());
if (instance == null) { if (instance == null) {
throw new ServiceException(ExceptionCons.NOT_FOUNT_INSTANCE); throw new ServiceException(ExceptionCons.NOT_FOUNT_INSTANCE);
@@ -305,10 +289,6 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
.handler(userIdStr) .handler(userIdStr)
.ignore(true); .ignore(true);
taskService.revoke(instance.getId(), flowParams); taskService.revoke(instance.getId(), flowParams);
} catch (Exception e) {
log.error("撤销失败: {}", e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
return true; return true;
} }
@@ -422,7 +402,6 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
if (flowInstance == null) { if (flowInstance == null) {
throw new ServiceException(ExceptionCons.NOT_FOUNT_INSTANCE); throw new ServiceException(ExceptionCons.NOT_FOUNT_INSTANCE);
} }
try {
Map<String, Object> variableMap = new HashMap<>(Optional.ofNullable(flowInstance.getVariableMap()).orElse(Collections.emptyMap())); Map<String, Object> variableMap = new HashMap<>(Optional.ofNullable(flowInstance.getVariableMap()).orElse(Collections.emptyMap()));
if (!variableMap.containsKey(bo.getKey())) { if (!variableMap.containsKey(bo.getKey())) {
log.error("变量不存在: {}", bo.getKey()); log.error("变量不存在: {}", bo.getKey());
@@ -430,12 +409,7 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
} }
variableMap.put(bo.getKey(), bo.getValue()); variableMap.put(bo.getKey(), bo.getValue());
flowInstance.setVariable(FlowEngine.jsonConvert.objToStr(variableMap)); flowInstance.setVariable(FlowEngine.jsonConvert.objToStr(variableMap));
flowInstanceMapper.updateById(flowInstance); return flowInstanceMapper.updateById(flowInstance) > 0;
} catch (Exception e) {
log.error("设置流程变量失败: {}", e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
return true;
} }
/** /**
@@ -480,7 +454,6 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean processInvalid(FlowInvalidBo bo) { public boolean processInvalid(FlowInvalidBo bo) {
try {
Instance instance = insService.getById(bo.getId()); Instance instance = insService.getById(bo.getId());
if (instance != null) { if (instance != null) {
BusinessStatusEnum.checkInvalidStatus(instance.getFlowStatus()); BusinessStatusEnum.checkInvalidStatus(instance.getFlowStatus());
@@ -492,9 +465,5 @@ public class FlwInstanceServiceImpl implements IFlwInstanceService {
.ignore(true); .ignore(true);
taskService.terminationByInsId(bo.getId(), flowParams); taskService.terminationByInsId(bo.getId(), flowParams);
return true; return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
} }
} }

View File

@@ -150,12 +150,7 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
.flowCode(startProcessBo.getFlowCode()) .flowCode(startProcessBo.getFlowCode())
.variable(startProcessBo.getVariables()) .variable(startProcessBo.getVariables())
.flowStatus(BusinessStatusEnum.DRAFT.getStatus()); .flowStatus(BusinessStatusEnum.DRAFT.getStatus());
Instance instance; Instance instance = insService.start(businessId, flowParams);
try {
instance = insService.start(businessId, flowParams);
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
// 保存流程实例业务信息 // 保存流程实例业务信息
this.buildFlowInstanceBizExt(instance, bizExt); this.buildFlowInstanceBizExt(instance, bizExt);
// 申请人执行流程 // 申请人执行流程
@@ -202,7 +197,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean completeTask(CompleteTaskBo completeTaskBo) { public boolean completeTask(CompleteTaskBo completeTaskBo) {
try {
// 获取任务ID并查询对应的流程任务和实例信息 // 获取任务ID并查询对应的流程任务和实例信息
Long taskId = completeTaskBo.getTaskId(); Long taskId = completeTaskBo.getTaskId();
List<String> messageType = completeTaskBo.getMessageType(); List<String> messageType = completeTaskBo.getMessageType();
@@ -244,10 +238,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
Boolean autoPass = Convert.toBool(variables.getOrDefault(AUTO_PASS, false)); Boolean autoPass = Convert.toBool(variables.getOrDefault(AUTO_PASS, false));
skipTask(taskId, flowParams, flowTask.getInstanceId(), autoPass); skipTask(taskId, flowParams, flowTask.getInstanceId(), autoPass);
return true; return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
} }
/** /**
@@ -478,7 +468,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean backProcess(BackProcessBo bo) { public boolean backProcess(BackProcessBo bo) {
try {
Long taskId = bo.getTaskId(); Long taskId = bo.getTaskId();
String notice = bo.getNotice(); String notice = bo.getNotice();
List<String> messageType = bo.getMessageType(); List<String> messageType = bo.getMessageType();
@@ -508,10 +497,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
.hisTaskExt(bo.getFileId()); .hisTaskExt(bo.getFileId());
taskService.skip(task.getId(), flowParams); taskService.skip(task.getId(), flowParams);
return true; return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
} }
/** /**
@@ -565,7 +550,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean terminationTask(FlowTerminationBo bo) { public boolean terminationTask(FlowTerminationBo bo) {
try {
Long taskId = bo.getTaskId(); Long taskId = bo.getTaskId();
Task task = taskService.getById(taskId); Task task = taskService.getById(taskId);
if (task == null) { if (task == null) {
@@ -581,10 +565,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
.hisStatus(TaskStatusEnum.TERMINATION.getStatus()); .hisStatus(TaskStatusEnum.TERMINATION.getStatus());
taskService.termination(taskId, flowParams); taskService.termination(taskId, flowParams);
return true; return true;
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
} }
/** /**
@@ -806,7 +786,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
if (CollUtil.isEmpty(taskIdList)) { if (CollUtil.isEmpty(taskIdList)) {
return false; return false;
} }
try {
List<FlowTask> flowTasks = this.selectByIdList(taskIdList); List<FlowTask> flowTasks = this.selectByIdList(taskIdList);
// 批量删除现有任务的办理人记录 // 批量删除现有任务的办理人记录
if (CollUtil.isNotEmpty(flowTasks)) { if (CollUtil.isNotEmpty(flowTasks)) {
@@ -820,10 +799,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
FlowEngine.userService().saveBatch(userList); FlowEngine.userService().saveBatch(userList);
} }
} }
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
return true; return true;
} }
@@ -862,7 +837,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
*/ */
@Override @Override
public boolean urgeTask(FlowUrgeTaskBo bo) { public boolean urgeTask(FlowUrgeTaskBo bo) {
try {
if (CollUtil.isEmpty(bo.getTaskIdList())) { if (CollUtil.isEmpty(bo.getTaskIdList())) {
return false; return false;
} }
@@ -873,10 +847,6 @@ public class FlwTaskServiceImpl implements IFlwTaskService {
List<String> messageType = bo.getMessageType(); List<String> messageType = bo.getMessageType();
String message = bo.getMessage(); String message = bo.getMessage();
flwCommonService.sendMessage(messageType, message, "单据审批提醒", userList); flwCommonService.sendMessage(messageType, message, "单据审批提醒", userList);
} catch (Exception e) {
log.error(e.getMessage(), e);
throw new ServiceException(e.getMessage());
}
return true; return true;
} }
} }

View File

@@ -6,7 +6,6 @@ import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.dto.CompleteTaskDTO; import org.dromara.common.core.domain.dto.CompleteTaskDTO;
import org.dromara.common.core.domain.dto.StartProcessDTO; import org.dromara.common.core.domain.dto.StartProcessDTO;
import org.dromara.common.core.domain.dto.StartProcessReturnDTO; import org.dromara.common.core.domain.dto.StartProcessReturnDTO;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.service.WorkflowService; import org.dromara.common.core.service.WorkflowService;
import org.dromara.common.core.utils.StringUtils; import org.dromara.common.core.utils.StringUtils;
import org.dromara.warm.flow.orm.entity.FlowInstance; import org.dromara.warm.flow.orm.entity.FlowInstance;
@@ -161,7 +160,6 @@ public class WorkflowServiceImpl implements WorkflowService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public boolean startCompleteTask(StartProcessDTO startProcess) { public boolean startCompleteTask(StartProcessDTO startProcess) {
try {
StartProcessBo processBo = new StartProcessBo(); StartProcessBo processBo = new StartProcessBo();
processBo.setBusinessId(startProcess.getBusinessId()); processBo.setBusinessId(startProcess.getBusinessId());
processBo.setFlowCode(startProcess.getFlowCode()); processBo.setFlowCode(startProcess.getFlowCode());
@@ -175,14 +173,6 @@ public class WorkflowServiceImpl implements WorkflowService {
taskBo.setMessageType(Collections.singletonList(MessageTypeEnum.SYSTEM_MESSAGE.getCode())); taskBo.setMessageType(Collections.singletonList(MessageTypeEnum.SYSTEM_MESSAGE.getCode()));
taskBo.setVariables(startProcess.getVariables()); taskBo.setVariables(startProcess.getVariables());
taskBo.setHandler(startProcess.getHandler()); taskBo.setHandler(startProcess.getHandler());
return flwTaskService.completeTask(taskBo);
boolean flag = flwTaskService.completeTask(taskBo);
if (!flag) {
throw new ServiceException("流程发起异常");
}
return true;
} catch (Exception e) {
throw new ServiceException(e.getMessage());
}
} }
} }