Skip to content

Commit

Permalink
feat(业务异常相关): 将所有手动抛出的异常调整为业务异常
Browse files Browse the repository at this point in the history
  • Loading branch information
houtaroy committed Oct 18, 2023
1 parent 4b7570d commit 6ef50a1
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cn.koala.attachment.repository.AttachmentRepository;
import cn.koala.attachment.storage.AttachmentStorage;
import cn.koala.web.BusinessException;
import jakarta.persistence.PreRemove;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
Expand Down Expand Up @@ -31,9 +32,9 @@ public AttachmentEntityListener(AttachmentRepository repository, AttachmentStora
@PreRemove
public void preDelete(AttachmentEntity entity) {
try {
storage.remove(this.repository.load(entity.getId()).orElseThrow(() -> new IllegalArgumentException("附件不存在")));
storage.remove(this.repository.load(entity.getId()).orElseThrow(() -> new BusinessException("附件不存在")));
} catch (Exception e) {
throw new IllegalStateException("附件删除失败, 请联系服务管理员", e);
throw new BusinessException("附件删除失败, 请联系服务管理员", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import cn.koala.attachment.AttachmentEntity;
import cn.koala.attachment.AttachmentService;
import cn.koala.attachment.storage.AttachmentStorage;
import cn.koala.web.BusinessException;
import cn.koala.web.DataResponse;
import cn.koala.web.Response;
import jakarta.servlet.ServletOutputStream;
Expand Down Expand Up @@ -61,7 +62,7 @@ public DataResponse<Attachment> upload(MultipartFile attachment) {
this.service.create(result);
return DataResponse.ok(result);
} catch (Exception e) {
throw new IllegalStateException("文件上传失败, 请联系服务管理员", e);
throw new BusinessException("文件上传失败, 请联系服务管理员", e);
}
}

Expand All @@ -78,7 +79,7 @@ public void download(Long id, HttpServletResponse response) {
IOUtils.copy(inputStream, outputStream);
}
} catch (Exception e) {
throw new IllegalStateException("文件下载失败, 请联系服务管理员", e);
throw new BusinessException("文件下载失败, 请联系服务管理员", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import cn.koala.template.Template;
import cn.koala.template.TemplateGroupService;
import cn.koala.toolkit.CompressHelper;
import cn.koala.web.BusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
Expand Down Expand Up @@ -45,7 +46,7 @@ public String download(Long databaseId, List<String> tableNames, Long templateGr
CompressHelper.compress(root, new File(downloadPath + result), ArchiveStreamFactory.ZIP);
return result;
} catch (Exception e) {
throw new IllegalStateException("生成代码文件失败", e);
throw new BusinessException("生成代码文件失败", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.koala.database.DatabaseTable;
import cn.koala.database.DatabaseTableColumn;
import cn.koala.toolkit.name.Name;
import cn.koala.web.BusinessException;

import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -71,7 +72,7 @@ private DomainProperty processDomainId(DatabaseTable table) {
.filter(column -> COLUMN_ID_NAME.equals(column.getName()))
.findFirst()
.map(this::processDomainProperty)
.orElseThrow(() -> new IllegalArgumentException("数据表必须包含名为id的主键列"));
.orElseThrow(() -> new BusinessException("数据表必须包含名为id的主键列"));
}

private List<DomainProperty> processDomainProperties(DatabaseTable table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import cn.koala.database.SimpleDatabaseTableColumn;
import cn.koala.database.repositories.DatabaseRepository;
import cn.koala.mybatis.AbstractMyBatisService;
import cn.koala.web.BusinessException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -35,7 +36,7 @@ public class DefaultDatabaseService extends AbstractMyBatisService<Database, Lon

@Override
public List<DatabaseTable> listTable(Long id) {
Database database = repository.load(id).orElseThrow(() -> new IllegalArgumentException("数据库不存在"));
Database database = repository.load(id).orElseThrow(() -> new BusinessException("数据库不存在"));
return query(database, (connection) -> {
ResultSet rs = connection.getMetaData().getTables(database.getCatalog(), database.getSchema(), null, new String[]{"TABLE"});
List<DatabaseTable> result = new ArrayList<>();
Expand All @@ -51,14 +52,14 @@ public List<DatabaseTable> listTable(Long id) {

@Override
public List<DatabaseTable> listTable(Long id, List<String> names) {
Database database = repository.load(id).orElseThrow(() -> new IllegalArgumentException("数据库不存在"));
Database database = repository.load(id).orElseThrow(() -> new BusinessException("数据库不存在"));
Assert.notEmpty(names, "表名列表不能为空");
return names.stream().map(name -> loadTable(database, name)).toList();
}

@Override
public DatabaseTable loadTable(Long id, String name) {
Database database = repository.load(id).orElseThrow(() -> new IllegalArgumentException("数据库不存在"));
Database database = repository.load(id).orElseThrow(() -> new BusinessException("数据库不存在"));
Assert.hasLength(name, "表名不能为空");
return loadTable(database, name);
}
Expand All @@ -67,7 +68,7 @@ protected DatabaseTable loadTable(Database database, String table) {
return query(database, (connection -> {
ResultSet rs = connection.getMetaData().getTables(database.getCatalog(), database.getSchema(), table, new String[]{"TABLE"});
if (!rs.next()) {
throw new IllegalArgumentException("表不存在");
throw new BusinessException("表不存在");
}
return SimpleDatabaseTable.builder()
.name(rs.getString("TABLE_NAME"))
Expand Down Expand Up @@ -123,7 +124,7 @@ protected <R> R query(Database database, ConnectionQuery<R> query) {
try (Connection connection = getConnection(database)) {
return query.query(connection);
} catch (SQLException e) {
throw new IllegalStateException("数据库操作异常", e);
throw new BusinessException("数据库操作异常", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.koala.ocr.OcrProcessor;
import cn.koala.ocr.OcrService;
import cn.koala.toolkit.MultipartFileHelper;
import cn.koala.web.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
Expand Down Expand Up @@ -44,10 +45,10 @@ public List<DetectedObjects.DetectedObject> image(MultipartFile file) {
return processor.process(OpenCVImageFactory.getInstance().fromInputStream(file.getInputStream())).items();
} catch (IOException e) {
LOGGER.error("上传文件[name={}]读取失败", file.getOriginalFilename(), e);
throw new IllegalArgumentException("上传文件读取失败");
throw new BusinessException("上传文件读取失败");
} catch (Exception e) {
LOGGER.error("图片识别失败", e);
throw new IllegalStateException("图片识别失败");
throw new BusinessException("图片识别失败");
}
}

Expand All @@ -69,10 +70,10 @@ public List<String> pdf(MultipartFile file) {
}
} catch (IOException e) {
LOGGER.error("上传文件[name={}]读取失败", file.getOriginalFilename(), e);
throw new IllegalArgumentException("上传文件读取失败");
throw new BusinessException("上传文件读取失败");
} catch (Exception e) {
LOGGER.error("PDF识别失败", e);
throw new IllegalStateException("PDF识别失败");
throw new BusinessException("PDF识别失败");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import cn.koala.query.Query;
import cn.koala.query.QueryService;
import cn.koala.query.repository.QueryRepository;
import cn.koala.web.BusinessException;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -29,7 +30,7 @@ public class JdbcQueryService extends AbstractMyBatisService<Query, Long> implem

@Override
public Page<Map<String, Object>> execute(Long id, Map<String, Object> parameters, Pageable pageable) {
Query query = getRepository().load(id).orElseThrow(() -> new IllegalArgumentException("查询不存在"));
Query query = getRepository().load(id).orElseThrow(() -> new BusinessException("查询不存在"));
addPageableParameters(parameters, pageable);
resetNullParameters(parameters);
List<Map<String, Object>> result = jdbcTemplate.queryForList(query.getSql(), parameters);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.koala.sensitive.support;

import cn.koala.sensitive.SensitiveWordService;
import cn.koala.web.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

Expand Down Expand Up @@ -36,12 +37,12 @@ public List<String> list() {
@Override
public void add(String word) {
LOGGER.debug("[koala-sensitive-word]: 文件敏感词服务不允许新增敏感词");
throw new IllegalStateException();
throw new BusinessException("文件敏感词服务不允许新增敏感词");
}

@Override
public void delete(String word) {
LOGGER.debug("[koala-sensitive-word]: 文件敏感词服务不允许删除敏感词");
throw new IllegalStateException();
throw new BusinessException("文件敏感词服务不允许删除敏感词");
}
}

0 comments on commit 6ef50a1

Please sign in to comment.