From d989e8847fafaeea8f43d60c65c943b9c85a9b64 Mon Sep 17 00:00:00 2001 From: chenxudong Date: Tue, 14 Jan 2025 14:25:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96BizException?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manage/common/GlobalExceptionHandler.java | 4 +-- .../controller/EdFileRelationController.java | 2 +- .../manage/controller/RoleController.java | 4 +-- .../service/serviceimpl/CommonService.java | 7 ++--- .../serviceimpl/EdFileInfoServiceImpl.java | 29 ++++++++++++------- .../EdFileRelationServiceImpl.java | 4 +-- .../service/serviceimpl/EdPrjServiceImpl.java | 16 +++++----- .../service/serviceimpl/RoleServiceImpl.java | 4 +-- .../service/serviceimpl/UserServiceImpl.java | 2 +- .../common/exception/BizException.java | 21 +++++++------- .../software/common/util/EleCommonUtil.java | 2 +- 11 files changed, 50 insertions(+), 45 deletions(-) diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/common/GlobalExceptionHandler.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/common/GlobalExceptionHandler.java index f256a8f..a079aba 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/common/GlobalExceptionHandler.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/common/GlobalExceptionHandler.java @@ -30,11 +30,11 @@ public class GlobalExceptionHandler { @ResponseBody public ElectromagneticResult bizError(BizException e) { log.error(e.getMessage(), e); - return ElectromagneticResultUtil.fail("-1", e.getMsg()); + return ElectromagneticResultUtil.fail("-1", e.getMessage()); } @ExceptionHandler(PermissionDeniedException.class) - @ResponseStatus(HttpStatus.FORBIDDEN) + @ResponseStatus(HttpStatus.OK) @ResponseBody public ElectromagneticResult accessDeniedError(PermissionDeniedException e) { log.error(e.getMessage(), e); diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/controller/EdFileRelationController.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/controller/EdFileRelationController.java index 29f3e90..769193f 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/controller/EdFileRelationController.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/controller/EdFileRelationController.java @@ -32,7 +32,7 @@ public class EdFileRelationController { Boolean res = edFileRelationService.createRelation(relation); return ElectromagneticResultUtil.success(res); } catch (BizException e) { - return ElectromagneticResultUtil.fail("500", e.getMsg()); + return ElectromagneticResultUtil.fail("500", e.getMessage()); } } diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/controller/RoleController.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/controller/RoleController.java index de951c8..1fa8b4d 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/controller/RoleController.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/controller/RoleController.java @@ -28,7 +28,7 @@ public class RoleController { Boolean res = roleService.createRole(roleDTO); return ElectromagneticResultUtil.success(res); } catch (BizException e) { - return ElectromagneticResultUtil.fail("400", e.getMsg()); + return ElectromagneticResultUtil.fail("400", e.getMessage()); } } @@ -40,7 +40,7 @@ public class RoleController { Boolean res = roleService.updateRole(roleDTO); return ElectromagneticResultUtil.success(res); } catch (BizException e) { - return ElectromagneticResultUtil.fail("400", e.getMsg()); + return ElectromagneticResultUtil.fail("400", e.getMessage()); } } diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/CommonService.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/CommonService.java index 2ed10cc..6edd810 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/CommonService.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/CommonService.java @@ -111,7 +111,6 @@ public class CommonService { } private String getDbPath(List ids) { - // TODO cache LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) .select(EdFileInfo::getId, EdFileInfo::getFileName, EdFileInfo::getDataType, EdFileInfo::getFileType, EdFileInfo::getFileCode) .in(EdFileInfo::getId, ids); @@ -211,7 +210,7 @@ public class CommonService { } catch (Exception e) { String info = "添加失败"; log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } } @@ -274,7 +273,7 @@ public class CommonService { } catch (Exception e) { String info = "查询项目失败"; log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } } @@ -343,7 +342,7 @@ public class CommonService { } catch (Exception e) { String info = "删除子集异常"; log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } } diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileInfoServiceImpl.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileInfoServiceImpl.java index 4869400..4bd9dd2 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileInfoServiceImpl.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileInfoServiceImpl.java @@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.electromagnetic.industry.software.common.enums.*; import com.electromagnetic.industry.software.common.exception.BizException; +import com.electromagnetic.industry.software.common.exception.PermissionDeniedException; import com.electromagnetic.industry.software.common.resp.ElectromagneticResult; import com.electromagnetic.industry.software.common.util.*; import com.electromagnetic.industry.software.manage.mapper.EdFileInfoMapper; @@ -90,6 +91,12 @@ public class EdFileInfoServiceImpl extends ServiceImpl queryEdFileInfo(FileInfoQueryDTO pars) { + String parentId = pars.getParentId(); + List accessibleTree = permissionService.getAccessibleTree(); + if (!accessibleTree.contains(parentId)) { + throw new PermissionDeniedException(); + } + LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) .select() .eq(EdFileInfo::getSaveStatus, EleDataSaveStatusEnum.SUCCESS.code) @@ -236,7 +243,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl map = edFileInfos.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e)); if (map.size() == 1) { if (!map.values().iterator().next().getId().equals(updateFileInfoDTO.getId())) { - throw new BizException(-1, "文件名已经存在"); + throw new BizException("文件名已经存在"); } } else if (map.size() > 1) { log.warn("数据库中存在了两份同名同后缀的文件"); - throw new BizException(-1, "文件名已经存在"); + throw new BizException("文件名已经存在"); } String srcFilePath = commonService.getFileSysPath(fileInfo.getFilePath()); @@ -297,7 +304,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl FileUtil.del(e.getAbsolutePath())); diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileRelationServiceImpl.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileRelationServiceImpl.java index 81a0da6..20eeffa 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileRelationServiceImpl.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/EdFileRelationServiceImpl.java @@ -63,7 +63,7 @@ public class EdFileRelationServiceImpl extends ServiceImpl list = this.list(queryWrapper); if (!list.isEmpty()) { - throw new BizException(-1, "请勿重复建立关系"); + throw new BizException("请勿重复建立关系"); } edFileRelation.setId(IdWorker.getSnowFlakeIdString()); @@ -256,7 +256,7 @@ public class EdFileRelationServiceImpl extends ServiceImpl } catch (Exception e) { String info = StrFormatter.format("文件创建失败,具体为--->{}", e.getMessage()); log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } return ElectromagneticResultUtil.success(true); } @@ -152,7 +152,7 @@ public class EdPrjServiceImpl extends ServiceImpl } catch (Exception e) { String info = StrFormatter.format("修改工程名异常--->{},{}", newPrjName, e.getMessage()); log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } return ElectromagneticResultUtil.success(true); } @@ -189,7 +189,7 @@ public class EdPrjServiceImpl extends ServiceImpl } catch (Exception e) { String info = "删除项目失败"; log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } } @@ -256,7 +256,7 @@ public class EdPrjServiceImpl extends ServiceImpl } catch (Exception e) { String info = "子集重排序异常"; log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } } @@ -291,7 +291,7 @@ public class EdPrjServiceImpl extends ServiceImpl } catch (Exception e) { String info = "项目发布异常"; log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } } @@ -379,7 +379,7 @@ public class EdPrjServiceImpl extends ServiceImpl } else { String info = "存在相同子集"; log.error(info); - throw new BizException(-1, info); + throw new BizException(info); } } } @@ -390,7 +390,7 @@ public class EdPrjServiceImpl extends ServiceImpl } catch (Exception e) { String info = StrFormatter.format("层级沿用失败"); log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } } @@ -425,7 +425,7 @@ public class EdPrjServiceImpl extends ServiceImpl } catch (Exception e) { String info = StrFormatter.format("修改子集名称为{}失败", newFolderName); log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } } diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/RoleServiceImpl.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/RoleServiceImpl.java index 7fdaa8d..74ea507 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/RoleServiceImpl.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/RoleServiceImpl.java @@ -68,7 +68,7 @@ public class RoleServiceImpl extends ServiceImpl implements Ro if (!checkRoleNameUnique(roleDTO)) { String info = "当前角色名称已存在:" + roleDTO.getRoleName(); log.error(info); - throw new BizException(-1, info); + throw new BizException(info); } // 创建角色 @@ -104,7 +104,7 @@ public class RoleServiceImpl extends ServiceImpl implements Ro if (!checkRoleNameUnique(roleDTO)) { String info = "当前角色名称已存在:" + roleDTO.getRoleName(); log.error(info); - throw new BizException(-1, info); + throw new BizException(info); } // 更新角色信息 diff --git a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/UserServiceImpl.java b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/UserServiceImpl.java index 455cf4f..0d02056 100644 --- a/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/UserServiceImpl.java +++ b/electrmangnetic/src/main/java/com/electromagnetic/industry/software/manage/service/serviceimpl/UserServiceImpl.java @@ -275,7 +275,7 @@ public class UserServiceImpl implements UserService { @Override public Boolean bindRoles(List list) { if (list == null || list.isEmpty()) { - throw new BizException(-1, "请勿发布空数据"); + throw new BizException("请勿发布空数据"); } for (UserBindRoleDTO dto : list) { activeRole(dto); diff --git a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/exception/BizException.java b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/exception/BizException.java index ef5f2a6..f49c721 100644 --- a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/exception/BizException.java +++ b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/exception/BizException.java @@ -1,17 +1,16 @@ package com.electromagnetic.industry.software.common.exception; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.NoArgsConstructor; - -@EqualsAndHashCode(callSuper = true) -@Data -@AllArgsConstructor -@NoArgsConstructor public class BizException extends RuntimeException { - private int code; - private String msg; + public BizException(final String message) { + super(message); + } + public BizException(final String message, final Throwable cause) { + super(message, cause); + } + + public BizException() { + super("请求错误,请联系管理员"); + } } diff --git a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/EleCommonUtil.java b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/EleCommonUtil.java index aaa4f96..50f26d1 100644 --- a/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/EleCommonUtil.java +++ b/electromagnetic-common/src/main/java/com/electromagnetic/industry/software/common/util/EleCommonUtil.java @@ -52,7 +52,7 @@ public final class EleCommonUtil { } catch (Exception e) { String info = "文件加密失败"; log.error(info, e); - throw new BizException(-1, info); + throw new BizException(info); } finally { FileUtil.del(filePath); FileUtil.move(new File(tmpPath), new File(filePath), true);