refactor(exception): 优化异常处理和日志记录

- 修改了 DeptServiceTest 中的断言方式
- 更新了 ExceptionUtil 中的异常处理逻辑,增加了更详细的错误提示
- 在 RoleServiceImpl 中添加了 @Transactional 注解和空值过滤
- 在 V1WorkflowController 中引入了 ExceptionUtil 进行异常处理
This commit is contained in:
zhuangtianxiang 2025-05-11 17:42:45 +08:00
parent 1e40e3fbca
commit cdf5842f01
4 changed files with 21 additions and 5 deletions

View File

@ -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> T difyNotFoundException(Supplier<T> supplier) throws ApiException {
try {
return supplier.get();
} catch (RuntimeException e) {
System.err.println(e.getMessage());
throw new ApiException("数据不存在,请检查传递参数是否错误!");
}
}
}

View File

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

View File

@ -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<RoleRepository, Role> implement
private final RoleRepository roleRepository;
@Override
@Transactional
public Role create(RoleDto dto) {
boolean existsByName = count(new LambdaQueryWrapper<Role>().eq(Role::getName, dto.getName())) > 0;
if (existsByName) {
@ -50,8 +52,11 @@ public class RoleServiceImpl extends ServiceImpl<RoleRepository, Role> 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;
}

View File

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