1
0

refactor(exception): 将内部异常类抽取为公共异常类

将 SimpleServiceSupport 中的静态内部异常类抽取到 common 模块的独立异常类文件中,包括:
- IdNotFoundException: ID 未找到异常
- NotComparableException: 不可比较异常
- NotCollectionException: 非集合异常
- NotStringException: 非字符串异常
This commit is contained in:
2026-01-07 11:17:03 +08:00
parent 5bf6e9ecdc
commit d08f9db9ac
5 changed files with 32 additions and 68 deletions

View File

@@ -0,0 +1,7 @@
package com.lanyuanxiaoyao.service.template.common.exception;
public class IdNotFoundException extends RuntimeException {
public IdNotFoundException(Long id) {
super("ID为 %d 的资源不存在".formatted(id));
}
}

View File

@@ -0,0 +1,7 @@
package com.lanyuanxiaoyao.service.template.common.exception;
public class NotCollectionException extends RuntimeException {
public NotCollectionException(String variable) {
super("变量 %s 不是集合".formatted(variable));
}
}

View File

@@ -0,0 +1,7 @@
package com.lanyuanxiaoyao.service.template.common.exception;
public class NotComparableException extends RuntimeException {
public NotComparableException(String variable) {
super("变量 %s 不能比较".formatted(variable));
}
}

View File

@@ -0,0 +1,7 @@
package com.lanyuanxiaoyao.service.template.common.exception;
public class NotStringException extends RuntimeException {
public NotStringException(String variable) {
super("变量 %s 不是字符串".formatted(variable));
}
}

View File

@@ -1,6 +1,10 @@
package com.lanyuanxiaoyao.service.template.jpa.service; package com.lanyuanxiaoyao.service.template.jpa.service;
import com.lanyuanxiaoyao.service.template.common.controller.Query; import com.lanyuanxiaoyao.service.template.common.controller.Query;
import com.lanyuanxiaoyao.service.template.common.exception.IdNotFoundException;
import com.lanyuanxiaoyao.service.template.common.exception.NotCollectionException;
import com.lanyuanxiaoyao.service.template.common.exception.NotComparableException;
import com.lanyuanxiaoyao.service.template.common.exception.NotStringException;
import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper; import com.lanyuanxiaoyao.service.template.common.helper.ObjectHelper;
import com.lanyuanxiaoyao.service.template.common.service.Page; import com.lanyuanxiaoyao.service.template.common.service.Page;
import com.lanyuanxiaoyao.service.template.common.service.SimpleService; import com.lanyuanxiaoyao.service.template.common.service.SimpleService;
@@ -594,72 +598,4 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
repository.deleteBatchByIds(ids); repository.deleteBatchByIds(ids);
} }
} }
/**
* ID未找到异常
* <p>
* 当根据ID查询实体但实体不存在时抛出此异常。
* </p>
*/
public static final class IdNotFoundException extends RuntimeException {
/**
* 构造函数带ID参数
*
* @param id 实体ID
*/
public IdNotFoundException(Long id) {
super("ID为 %d 的资源不存在".formatted(id));
}
}
/**
* 不可比较异常
* <p>
* 当尝试对不可比较的字段或值执行比较操作时抛出此异常。
* </p>
*/
public static final class NotComparableException extends RuntimeException {
/**
* 构造函数
*
* @param variable 变量名称
*/
public NotComparableException(String variable) {
super("变量 %s 不能比较".formatted(variable));
}
}
/**
* 非集合异常
* <p>
* 当尝试对非集合类型的字段或值执行集合操作时抛出此异常。
* </p>
*/
public static final class NotCollectionException extends RuntimeException {
/**
* 构造函数
*
* @param variable 变量名称
*/
public NotCollectionException(String variable) {
super("变量 %s 不是集合".formatted(variable));
}
}
/**
* 非字符串异常
* <p>
* 当尝试对非字符串类型的字段或值执行字符串操作时抛出此异常。
* </p>
*/
public static final class NotStringException extends RuntimeException {
/**
* 构造函数
*
* @param variable 变量名称
*/
public NotStringException(String variable) {
super("变量 %s 不是字符串".formatted(variable));
}
}
} }