优化BizException

This commit is contained in:
chenxudong 2025-01-14 14:25:08 +08:00
parent 70a05e23c3
commit d989e8847f
11 changed files with 50 additions and 45 deletions

View File

@ -30,11 +30,11 @@ public class GlobalExceptionHandler {
@ResponseBody @ResponseBody
public ElectromagneticResult<?> bizError(BizException e) { public ElectromagneticResult<?> bizError(BizException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);
return ElectromagneticResultUtil.fail("-1", e.getMsg()); return ElectromagneticResultUtil.fail("-1", e.getMessage());
} }
@ExceptionHandler(PermissionDeniedException.class) @ExceptionHandler(PermissionDeniedException.class)
@ResponseStatus(HttpStatus.FORBIDDEN) @ResponseStatus(HttpStatus.OK)
@ResponseBody @ResponseBody
public ElectromagneticResult<?> accessDeniedError(PermissionDeniedException e) { public ElectromagneticResult<?> accessDeniedError(PermissionDeniedException e) {
log.error(e.getMessage(), e); log.error(e.getMessage(), e);

View File

@ -32,7 +32,7 @@ public class EdFileRelationController {
Boolean res = edFileRelationService.createRelation(relation); Boolean res = edFileRelationService.createRelation(relation);
return ElectromagneticResultUtil.success(res); return ElectromagneticResultUtil.success(res);
} catch (BizException e) { } catch (BizException e) {
return ElectromagneticResultUtil.fail("500", e.getMsg()); return ElectromagneticResultUtil.fail("500", e.getMessage());
} }
} }

View File

@ -28,7 +28,7 @@ public class RoleController {
Boolean res = roleService.createRole(roleDTO); Boolean res = roleService.createRole(roleDTO);
return ElectromagneticResultUtil.success(res); return ElectromagneticResultUtil.success(res);
} catch (BizException e) { } 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); Boolean res = roleService.updateRole(roleDTO);
return ElectromagneticResultUtil.success(res); return ElectromagneticResultUtil.success(res);
} catch (BizException e) { } catch (BizException e) {
return ElectromagneticResultUtil.fail("400", e.getMsg()); return ElectromagneticResultUtil.fail("400", e.getMessage());
} }
} }

View File

@ -111,7 +111,6 @@ public class CommonService {
} }
private String getDbPath(List<String> ids) { private String getDbPath(List<String> ids) {
// TODO cache
LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
.select(EdFileInfo::getId, EdFileInfo::getFileName, EdFileInfo::getDataType, EdFileInfo::getFileType, EdFileInfo::getFileCode) .select(EdFileInfo::getId, EdFileInfo::getFileName, EdFileInfo::getDataType, EdFileInfo::getFileType, EdFileInfo::getFileCode)
.in(EdFileInfo::getId, ids); .in(EdFileInfo::getId, ids);
@ -211,7 +210,7 @@ public class CommonService {
} catch (Exception e) { } catch (Exception e) {
String info = "添加失败"; String info = "添加失败";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
@ -274,7 +273,7 @@ public class CommonService {
} catch (Exception e) { } catch (Exception e) {
String info = "查询项目失败"; String info = "查询项目失败";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
@ -343,7 +342,7 @@ public class CommonService {
} catch (Exception e) { } catch (Exception e) {
String info = "删除子集异常"; String info = "删除子集异常";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }

View File

@ -20,6 +20,7 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.electromagnetic.industry.software.common.enums.*; import com.electromagnetic.industry.software.common.enums.*;
import com.electromagnetic.industry.software.common.exception.BizException; 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.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.*; import com.electromagnetic.industry.software.common.util.*;
import com.electromagnetic.industry.software.manage.mapper.EdFileInfoMapper; import com.electromagnetic.industry.software.manage.mapper.EdFileInfoMapper;
@ -90,6 +91,12 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
@Override @Override
public ElectromagneticResult<?> queryEdFileInfo(FileInfoQueryDTO pars) { public ElectromagneticResult<?> queryEdFileInfo(FileInfoQueryDTO pars) {
String parentId = pars.getParentId();
List<String> accessibleTree = permissionService.getAccessibleTree();
if (!accessibleTree.contains(parentId)) {
throw new PermissionDeniedException();
}
LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class) LambdaQueryWrapper<EdFileInfo> queryWrapper = Wrappers.lambdaQuery(EdFileInfo.class)
.select() .select()
.eq(EdFileInfo::getSaveStatus, EleDataSaveStatusEnum.SUCCESS.code) .eq(EdFileInfo::getSaveStatus, EleDataSaveStatusEnum.SUCCESS.code)
@ -236,7 +243,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
} catch (Exception e) { } catch (Exception e) {
String info = "下载文件异常"; String info = "下载文件异常";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
@ -275,11 +282,11 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
Map<String, EdFileInfo> map = edFileInfos.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e)); Map<String, EdFileInfo> map = edFileInfos.stream().collect(Collectors.toMap(EdFileInfo::getId, e -> e));
if (map.size() == 1) { if (map.size() == 1) {
if (!map.values().iterator().next().getId().equals(updateFileInfoDTO.getId())) { if (!map.values().iterator().next().getId().equals(updateFileInfoDTO.getId())) {
throw new BizException(-1, "文件名已经存在"); throw new BizException("文件名已经存在");
} }
} else if (map.size() > 1) { } else if (map.size() > 1) {
log.warn("数据库中存在了两份同名同后缀的文件"); log.warn("数据库中存在了两份同名同后缀的文件");
throw new BizException(-1, "文件名已经存在"); throw new BizException("文件名已经存在");
} }
String srcFilePath = commonService.getFileSysPath(fileInfo.getFilePath()); String srcFilePath = commonService.getFileSysPath(fileInfo.getFilePath());
@ -297,7 +304,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
} catch (Exception e) { } catch (Exception e) {
String info = "更新文件信息失败。"; String info = "更新文件信息失败。";
log.error(info, e); log.error(info, e);
throw new BizException(-1, e.getMessage()); throw new BizException(e.getMessage());
} }
} }
@ -323,7 +330,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
} catch (Exception e) { } catch (Exception e) {
String info = "版本回退失败。"; String info = "版本回退失败。";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
return ElectromagneticResultUtil.success(true); return ElectromagneticResultUtil.success(true);
} }
@ -377,7 +384,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
FileUtil.writeFromStream(fileChunkDTO.getFile().getInputStream(), destPath); FileUtil.writeFromStream(fileChunkDTO.getFile().getInputStream(), destPath);
} catch (IOException ioException) { } catch (IOException ioException) {
log.error("上传文件失败...", ioException); log.error("上传文件失败...", ioException);
throw new BizException(-1, "上传文件失败"); throw new BizException("上传文件失败");
} }
return ElectromagneticResultUtil.success(fileChunkDTO.getIdentifier()); return ElectromagneticResultUtil.success(fileChunkDTO.getIdentifier());
} }
@ -410,7 +417,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
} catch (Exception e) { } catch (Exception e) {
String info = "文件上传错误"; String info = "文件上传错误";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
String uuid = IdUtil.fastSimpleUUID(); String uuid = IdUtil.fastSimpleUUID();
String tmpDir = uploadDataDir + currentUserId + File.separator + uuid + File.separator; String tmpDir = uploadDataDir + currentUserId + File.separator + uuid + File.separator;
@ -673,7 +680,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
} catch (Exception e) { } catch (Exception e) {
String info = "导出失败。"; String info = "导出失败。";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} finally { } finally {
fileSystemService.deleteFile(exportZipFile, prjDirPath); fileSystemService.deleteFile(exportZipFile, prjDirPath);
} }
@ -770,7 +777,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
this.saveOrUpdate(newEdFileInfo); this.saveOrUpdate(newEdFileInfo);
String info = "上传文件失败"; String info = "上传文件失败";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
return ElectromagneticResultUtil.success(true); return ElectromagneticResultUtil.success(true);
} }
@ -1128,7 +1135,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
if (!FileUtil.exist(new File(tmpPath))) { if (!FileUtil.exist(new File(tmpPath))) {
String info = StrFormatter.format("第{}个分片没有上传完成,请上传完成后再合并。", i); String info = StrFormatter.format("第{}个分片没有上传完成,请上传完成后再合并。", i);
log.error(info); log.error(info);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
// 合并分片 // 合并分片
@ -1143,7 +1150,7 @@ public class EdFileInfoServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileI
FileUtil.del(destColibPath); FileUtil.del(destColibPath);
String info = "文件合并失败"; String info = "文件合并失败";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
Arrays.stream(partFiles).forEach(e -> FileUtil.del(e.getAbsolutePath())); Arrays.stream(partFiles).forEach(e -> FileUtil.del(e.getAbsolutePath()));

View File

@ -63,7 +63,7 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
.eq(EdFileRelation::getId1, queryId2).eq(EdFileRelation::getId2, queryId1); .eq(EdFileRelation::getId1, queryId2).eq(EdFileRelation::getId2, queryId1);
List<EdFileRelation> list = this.list(queryWrapper); List<EdFileRelation> list = this.list(queryWrapper);
if (!list.isEmpty()) { if (!list.isEmpty()) {
throw new BizException(-1, "请勿重复建立关系"); throw new BizException("请勿重复建立关系");
} }
edFileRelation.setId(IdWorker.getSnowFlakeIdString()); edFileRelation.setId(IdWorker.getSnowFlakeIdString());
@ -256,7 +256,7 @@ public class EdFileRelationServiceImpl extends ServiceImpl<EdFileRelationMapper,
edFileInfoService.saveOrUpdate(newEdFileInfo); edFileInfoService.saveOrUpdate(newEdFileInfo);
String info = "上传文件失败"; String info = "上传文件失败";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
return ElectromagneticResultUtil.success(true); return ElectromagneticResultUtil.success(true);
} }

View File

@ -106,7 +106,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
} catch (Exception e) { } catch (Exception e) {
String info = StrFormatter.format("文件创建失败,具体为--->{}", e.getMessage()); String info = StrFormatter.format("文件创建失败,具体为--->{}", e.getMessage());
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
return ElectromagneticResultUtil.success(true); return ElectromagneticResultUtil.success(true);
} }
@ -152,7 +152,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
} catch (Exception e) { } catch (Exception e) {
String info = StrFormatter.format("修改工程名异常--->{}{}", newPrjName, e.getMessage()); String info = StrFormatter.format("修改工程名异常--->{}{}", newPrjName, e.getMessage());
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
return ElectromagneticResultUtil.success(true); return ElectromagneticResultUtil.success(true);
} }
@ -189,7 +189,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
} catch (Exception e) { } catch (Exception e) {
String info = "删除项目失败"; String info = "删除项目失败";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
@ -256,7 +256,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
} catch (Exception e) { } catch (Exception e) {
String info = "子集重排序异常"; String info = "子集重排序异常";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
@ -291,7 +291,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
} catch (Exception e) { } catch (Exception e) {
String info = "项目发布异常"; String info = "项目发布异常";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
@ -379,7 +379,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
} else { } else {
String info = "存在相同子集"; String info = "存在相同子集";
log.error(info); log.error(info);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
} }
@ -390,7 +390,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
} catch (Exception e) { } catch (Exception e) {
String info = StrFormatter.format("层级沿用失败"); String info = StrFormatter.format("层级沿用失败");
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }
@ -425,7 +425,7 @@ public class EdPrjServiceImpl extends ServiceImpl<EdFileInfoMapper, EdFileInfo>
} catch (Exception e) { } catch (Exception e) {
String info = StrFormatter.format("修改子集名称为{}失败", newFolderName); String info = StrFormatter.format("修改子集名称为{}失败", newFolderName);
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} }
} }

View File

@ -68,7 +68,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
if (!checkRoleNameUnique(roleDTO)) { if (!checkRoleNameUnique(roleDTO)) {
String info = "当前角色名称已存在:" + roleDTO.getRoleName(); String info = "当前角色名称已存在:" + roleDTO.getRoleName();
log.error(info); log.error(info);
throw new BizException(-1, info); throw new BizException(info);
} }
// 创建角色 // 创建角色
@ -104,7 +104,7 @@ public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements Ro
if (!checkRoleNameUnique(roleDTO)) { if (!checkRoleNameUnique(roleDTO)) {
String info = "当前角色名称已存在:" + roleDTO.getRoleName(); String info = "当前角色名称已存在:" + roleDTO.getRoleName();
log.error(info); log.error(info);
throw new BizException(-1, info); throw new BizException(info);
} }
// 更新角色信息 // 更新角色信息

View File

@ -275,7 +275,7 @@ public class UserServiceImpl implements UserService {
@Override @Override
public Boolean bindRoles(List<UserBindRoleDTO> list) { public Boolean bindRoles(List<UserBindRoleDTO> list) {
if (list == null || list.isEmpty()) { if (list == null || list.isEmpty()) {
throw new BizException(-1, "请勿发布空数据"); throw new BizException("请勿发布空数据");
} }
for (UserBindRoleDTO dto : list) { for (UserBindRoleDTO dto : list) {
activeRole(dto); activeRole(dto);

View File

@ -1,17 +1,16 @@
package com.electromagnetic.industry.software.common.exception; 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 { public class BizException extends RuntimeException {
private int code; public BizException(final String message) {
private String msg; super(message);
}
public BizException(final String message, final Throwable cause) {
super(message, cause);
}
public BizException() {
super("请求错误,请联系管理员");
}
} }

View File

@ -52,7 +52,7 @@ public final class EleCommonUtil {
} catch (Exception e) { } catch (Exception e) {
String info = "文件加密失败"; String info = "文件加密失败";
log.error(info, e); log.error(info, e);
throw new BizException(-1, info); throw new BizException(info);
} finally { } finally {
FileUtil.del(filePath); FileUtil.del(filePath);
FileUtil.move(new File(tmpPath), new File(filePath), true); FileUtil.move(new File(tmpPath), new File(filePath), true);