diff --git a/src/main/java/com/huoran/iasf/common/exception/NotFoundException.java b/src/main/java/com/huoran/iasf/common/exception/NotFoundException.java new file mode 100644 index 0000000..84c2ebd --- /dev/null +++ b/src/main/java/com/huoran/iasf/common/exception/NotFoundException.java @@ -0,0 +1,20 @@ +package com.huoran.iasf.common.exception; + +import com.huoran.iasf.common.exception.code.BaseResponseCode; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * BusinessException + * + * @author cheney + * @version V1.0 + * @date 2022年7月28日 + */ +@Getter +@NoArgsConstructor +@AllArgsConstructor +public class NotFoundException extends RuntimeException { + private BaseResponseCode baseResponseCode; +} diff --git a/src/main/java/com/huoran/iasf/common/exception/code/BaseResponseCode.java b/src/main/java/com/huoran/iasf/common/exception/code/BaseResponseCode.java index 17b086f..bf576b6 100644 --- a/src/main/java/com/huoran/iasf/common/exception/code/BaseResponseCode.java +++ b/src/main/java/com/huoran/iasf/common/exception/code/BaseResponseCode.java @@ -51,7 +51,9 @@ public enum BaseResponseCode implements ResponseCodeInterface { OPERATION_MENU_PERMISSION_URL_NOT_NULL(401015, "菜单权限的url不能为空"), OPERATION_MENU_PERMISSION_URL_PERMS_NULL(401016, "菜单权限的标识符不能为空"), ROLE_ERROR(401017, "账号角色被删,请联系管理员添加角色后再试"), - EXCEL_FILE_NULL(40006, "导入失败,导入数据为空!") + EXCEL_FILE_NULL(40006, "导入失败,导入数据为空!"), + + DATA_DOES_NOT_EXIST(500, "当前数据不存在"), ; /** diff --git a/src/main/java/com/huoran/iasf/common/exception/handler/RestExceptionHandler.java b/src/main/java/com/huoran/iasf/common/exception/handler/RestExceptionHandler.java index d2eebb7..6a95f10 100644 --- a/src/main/java/com/huoran/iasf/common/exception/handler/RestExceptionHandler.java +++ b/src/main/java/com/huoran/iasf/common/exception/handler/RestExceptionHandler.java @@ -2,6 +2,7 @@ package com.huoran.iasf.common.exception.handler; import com.google.common.collect.Maps; import com.huoran.iasf.common.exception.BusinessException; +import com.huoran.iasf.common.exception.NotFoundException; import com.huoran.iasf.common.exception.code.BaseResponseCode; import com.huoran.iasf.common.utils.R; import lombok.extern.slf4j.Slf4j; @@ -69,6 +70,14 @@ public class RestExceptionHandler { return new R(em.getCode(), em.getMsg()); } + @ExceptionHandler(value = NotFoundException.class) + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + public R businessExceptionHandler(NotFoundException e) { + log.error("Exception,exception:{}", e, e); + BaseResponseCode em = e.getBaseResponseCode(); + return new R(em.getCode(), em.getMsg()); + } + /** * 没有权限 返回403视图 */ diff --git a/src/main/java/com/huoran/iasf/controller/SysContentController.java b/src/main/java/com/huoran/iasf/controller/SysContentController.java index 728d80b..8fab6b9 100644 --- a/src/main/java/com/huoran/iasf/controller/SysContentController.java +++ b/src/main/java/com/huoran/iasf/controller/SysContentController.java @@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.toolkit.SqlHelper; +import com.huoran.iasf.common.exception.NotFoundException; +import com.huoran.iasf.common.exception.code.BaseResponseCode; import com.huoran.iasf.entity.SysColumn; import com.huoran.iasf.entity.SysContentFile; import com.huoran.iasf.entity.SysFilesEntity; @@ -70,15 +72,24 @@ public class SysContentController { @PostMapping("/findById") @ApiOperation(value = "查询详情", response = SysContent.class) public R findById(@ApiParam(name = "id", value = "主键", required = true) @RequestParam Integer id) { + + SysContent sysContent = service.getById(id); + if (sysContent == null) { + throw new NotFoundException(BaseResponseCode.DATA_DOES_NOT_EXIST); + } SysColumn sysColumn = columnService.getById(sysContent.getColumnId()); + if (sysColumn == null) { + throw new NotFoundException(BaseResponseCode.DATA_DOES_NOT_EXIST); + } if (sysColumn.getColumnName() != null) { sysContent.setColumnName(sysColumn.getColumnName()); } List fileList = fileService.getFileByContentId(id); sysContent.setFileList(fileList); + return R.success(sysContent); } @@ -265,9 +276,7 @@ public class SysContentController { @PostMapping("/modifiedSort") @ApiOperation(value = "修改排序", response = SysContent.class) - public R modifiedSort( - @ApiParam(name = "sequenceNumber", value = "要排序的序号", required = true) @RequestParam Integer sequenceNumber, - @ApiParam(name = "articleId", value = "文章Id", required = true) @RequestParam Integer articleId) { + public R modifiedSort(@ApiParam(name = "sequenceNumber", value = "要排序的序号", required = true) @RequestParam Integer sequenceNumber, @ApiParam(name = "articleId", value = "文章Id", required = true) @RequestParam Integer articleId) { return service.modifiedSort(sequenceNumber, articleId); }