From cdf5842f01b3a63f86435409e3c564953eea7b23 Mon Sep 17 00:00:00 2001 From: zhuangtianxiang <2913129173@qq.com> Date: Sun, 11 May 2025 17:42:45 +0800 Subject: [PATCH] =?UTF-8?q?refactor(exception):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E5=92=8C=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改了 DeptServiceTest 中的断言方式 - 更新了 ExceptionUtil 中的异常处理逻辑,增加了更详细的错误提示 - 在 RoleServiceImpl 中添加了 @Transactional 注解和空值过滤 - 在 V1WorkflowController 中引入了 ExceptionUtil 进行异常处理 --- .../com/zsc/edu/dify/exception/ExceptionUtil.java | 11 ++++++++++- .../modules/dify/controller/V1WorkflowController.java | 5 +++-- .../modules/system/service/impl/RoleServiceImpl.java | 7 ++++++- .../zsc/edu/dify/service/system/DeptServiceTest.java | 3 ++- 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/zsc/edu/dify/exception/ExceptionUtil.java b/src/main/java/com/zsc/edu/dify/exception/ExceptionUtil.java index fa5af7d..8146fb6 100644 --- a/src/main/java/com/zsc/edu/dify/exception/ExceptionUtil.java +++ b/src/main/java/com/zsc/edu/dify/exception/ExceptionUtil.java @@ -12,7 +12,16 @@ public class ExceptionUtil { return supplier.get(); } catch (RuntimeException e) { System.err.println(e.getMessage()); - throw new ApiException("服务器错误"); + throw new ApiException("服务器错误,请联系工作人员!"); + } + } + + public static T difyNotFoundException(Supplier supplier) throws ApiException { + try { + return supplier.get(); + } catch (RuntimeException e) { + System.err.println(e.getMessage()); + throw new ApiException("数据不存在,请检查传递参数是否错误!"); } } } diff --git a/src/main/java/com/zsc/edu/dify/modules/dify/controller/V1WorkflowController.java b/src/main/java/com/zsc/edu/dify/modules/dify/controller/V1WorkflowController.java index 910bd96..177268e 100644 --- a/src/main/java/com/zsc/edu/dify/modules/dify/controller/V1WorkflowController.java +++ b/src/main/java/com/zsc/edu/dify/modules/dify/controller/V1WorkflowController.java @@ -1,6 +1,7 @@ package com.zsc.edu.dify.modules.dify.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.zsc.edu.dify.exception.ExceptionUtil; import com.zsc.edu.dify.framework.mybatisplus.DataPermission; import com.zsc.edu.dify.framework.security.SecurityUtil; import com.zsc.edu.dify.modules.dify.entity.WorkflowData; @@ -43,7 +44,7 @@ public class V1WorkflowController { @OperationLogAnnotation(content = "'dify工作流'", operationType = "运行") public WorkflowRunResponse runWorkflow(@RequestBody WorkflowRunRequest request, @PathVariable String appId) { request.setUserId(SecurityUtil.getUserInfo().id.toString()); - return difyWorkflowService.run(request, appId); + return ExceptionUtil.difyException(() -> difyWorkflowService.run(request, appId)); } /** @@ -80,7 +81,7 @@ public class V1WorkflowController { @PreAuthorize("hasAuthority('dify:workflow:info')") public WorkflowInfoResponse info(String workflowRunId, @PathVariable String appId) { String apiKey =appEntityService.getApikey(appId); - return difyWorkflow.info(workflowRunId, apiKey); + return ExceptionUtil.difyNotFoundException(() -> difyWorkflow.info(workflowRunId, apiKey)); } /** diff --git a/src/main/java/com/zsc/edu/dify/modules/system/service/impl/RoleServiceImpl.java b/src/main/java/com/zsc/edu/dify/modules/system/service/impl/RoleServiceImpl.java index 2dacda8..a6c3890 100644 --- a/src/main/java/com/zsc/edu/dify/modules/system/service/impl/RoleServiceImpl.java +++ b/src/main/java/com/zsc/edu/dify/modules/system/service/impl/RoleServiceImpl.java @@ -13,6 +13,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zsc.edu.dify.modules.system.vo.RoleVo; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.Objects; @@ -38,6 +39,7 @@ public class RoleServiceImpl extends ServiceImpl implement private final RoleRepository roleRepository; @Override + @Transactional public Role create(RoleDto dto) { boolean existsByName = count(new LambdaQueryWrapper().eq(Role::getName, dto.getName())) > 0; if (existsByName) { @@ -50,8 +52,11 @@ public class RoleServiceImpl extends ServiceImpl implement if (dto.getMenuIds() != null) { roleMenuRepository.insert( dto.getMenuIds().stream() - .map(menuId -> new RoleMenu(role.getId(), menuId)).collect(Collectors.toList())); + .filter(Objects::nonNull) + .map(menuId -> new RoleMenu(role.getId(), menuId)) + .collect(Collectors.toList())); } + return role; } diff --git a/src/test/java/com/zsc/edu/dify/service/system/DeptServiceTest.java b/src/test/java/com/zsc/edu/dify/service/system/DeptServiceTest.java index d3c70f4..b556463 100644 --- a/src/test/java/com/zsc/edu/dify/service/system/DeptServiceTest.java +++ b/src/test/java/com/zsc/edu/dify/service/system/DeptServiceTest.java @@ -93,7 +93,8 @@ class DeptServiceTest { assertEquals(tmp.getId(), dept2.id); // 不能改为其他已存在的同名同代码部门 assertThrows(ConstraintException.class, - () -> service.edit(new DeptDto(dept3.getName(), "remark",null), dept2.id)); + () -> service.edit( + new DeptDto(dept3.getName(), "remark",null), dept2.id)); }