refactor: 进一步完善异常信息国际化

This commit is contained in:
开源海哥 2023-07-30 19:47:12 +08:00
parent 80a542dc6f
commit 1a4234f754
11 changed files with 74 additions and 63 deletions

View File

@ -240,7 +240,7 @@ public interface BaseMapper<T> {
* @return 受影响的行数
*/
default int deleteByMap(Map<String, Object> whereConditions) {
FlexAssert.notEmpty(whereConditions, "deleteByMap is not allow empty map.");
FlexAssert.notEmpty(whereConditions, "whereConditions");
return deleteByQuery(QueryWrapper.create().where(whereConditions));
}
@ -251,7 +251,7 @@ public interface BaseMapper<T> {
* @return 受影响的行数
*/
default int deleteByCondition(QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
return deleteByQuery(QueryWrapper.create().where(whereConditions));
}
@ -296,7 +296,7 @@ public interface BaseMapper<T> {
* @return 受影响的行数
*/
default int updateByMap(T entity, Map<String, Object> whereConditions) {
FlexAssert.notEmpty(whereConditions, "updateByMap is not allow empty map.");
FlexAssert.notEmpty(whereConditions, "whereConditions");
return updateByQuery(entity, QueryWrapper.create().where(whereConditions));
}
@ -309,7 +309,7 @@ public interface BaseMapper<T> {
* @return 受影响的行数
*/
default int updateByMap(T entity, boolean ignoreNulls, Map<String, Object> whereConditions) {
FlexAssert.notEmpty(whereConditions, "updateByMap is not allow empty map.");
FlexAssert.notEmpty(whereConditions, "whereConditions");
return updateByQuery(entity, ignoreNulls, QueryWrapper.create().where(whereConditions));
}
@ -321,7 +321,7 @@ public interface BaseMapper<T> {
* @return 受影响的行数
*/
default int updateByCondition(T entity, QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
return updateByQuery(entity, QueryWrapper.create().where(whereConditions));
}
@ -334,7 +334,7 @@ public interface BaseMapper<T> {
* @return 受影响的行数
*/
default int updateByCondition(T entity, boolean ignoreNulls, QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
return updateByQuery(entity, ignoreNulls, QueryWrapper.create().where(whereConditions));
}
@ -385,7 +385,7 @@ public interface BaseMapper<T> {
*/
@Deprecated
default int updateNumberAddByQuery(QueryColumn column, Number value, QueryWrapper queryWrapper) {
FlexAssert.notNull(value, "add value can not be null.");
FlexAssert.notNull(value, "value");
return updateNumberAddByQuery(column.getName(), value, queryWrapper);
}
@ -400,7 +400,7 @@ public interface BaseMapper<T> {
*/
@Deprecated
default int updateNumberAddByQuery(LambdaGetter<T> fn, Number value, QueryWrapper queryWrapper) {
FlexAssert.notNull(value, "add value can not be null.");
FlexAssert.notNull(value, "value");
TableInfo tableInfo = TableInfoFactory.ofMapperClass(ClassUtil.getUsefulClass(getClass()));
String column = tableInfo.getColumnByProperty(LambdaUtil.getFieldName(fn));
return updateNumberAddByQuery(column, value, queryWrapper);
@ -425,7 +425,7 @@ public interface BaseMapper<T> {
* @return 实体类数据
*/
default T selectOneByMap(Map<String, Object> whereConditions) {
FlexAssert.notEmpty(whereConditions, "whereConditions map can not be null or empty.");
FlexAssert.notEmpty(whereConditions, "whereConditions");
return selectOneByQuery(QueryWrapper.create().where(whereConditions).limit(1));
}
@ -436,7 +436,7 @@ public interface BaseMapper<T> {
* @return 实体类数据
*/
default T selectOneByCondition(QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
return selectOneByQuery(QueryWrapper.create().where(whereConditions).limit(1));
}
@ -468,7 +468,7 @@ public interface BaseMapper<T> {
* @return 实体类数据
*/
default T selectOneWithRelationsByMap(Map<String, Object> whereConditions) {
FlexAssert.notEmpty(whereConditions, "whereConditions map can not be null or empty.");
FlexAssert.notEmpty(whereConditions, "whereConditions");
return selectOneWithRelationsByQuery(QueryWrapper.create().where(whereConditions).limit(1));
}
@ -479,7 +479,7 @@ public interface BaseMapper<T> {
* @return 实体类数据
*/
default T selectOneWithRelationsByCondition(QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
return selectOneWithRelationsByQuery(QueryWrapper.create().where(whereConditions).limit(1));
}
@ -545,7 +545,7 @@ public interface BaseMapper<T> {
* @return 数据列表
*/
default List<T> selectListByMap(Map<String, Object> whereConditions) {
FlexAssert.notEmpty(whereConditions, "whereConditions map can not be null or empty.");
FlexAssert.notEmpty(whereConditions, "whereConditions");
return selectListByQuery(QueryWrapper.create().where(whereConditions));
}
@ -557,7 +557,7 @@ public interface BaseMapper<T> {
* @return 数据列表
*/
default List<T> selectListByMap(Map<String, Object> whereConditions, int count) {
FlexAssert.notEmpty(whereConditions, "whereConditions map can not be null or empty.");
FlexAssert.notEmpty(whereConditions, "whereConditions");
return selectListByQuery(QueryWrapper.create().where(whereConditions).limit(count));
}
@ -568,7 +568,7 @@ public interface BaseMapper<T> {
* @return 数据列表
*/
default List<T> selectListByCondition(QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
return selectListByQuery(QueryWrapper.create().where(whereConditions));
}
@ -580,7 +580,7 @@ public interface BaseMapper<T> {
* @return 数据列表
*/
default List<T> selectListByCondition(QueryCondition whereConditions, int count) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
return selectListByQuery(QueryWrapper.create().where(whereConditions).limit(count));
}
@ -849,7 +849,7 @@ public interface BaseMapper<T> {
* @return 数据量
*/
default long selectCountByCondition(QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
return selectCountByQuery(QueryWrapper.create().where(whereConditions));
}
@ -943,7 +943,7 @@ public interface BaseMapper<T> {
* @return 分页数据
*/
default Page<T> paginate(int pageNumber, int pageSize, int totalRow, QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
Page<T> page = new Page<>(pageNumber, pageSize, totalRow);
return paginate(page, new QueryWrapper().where(whereConditions));
}
@ -958,7 +958,7 @@ public interface BaseMapper<T> {
* @return 分页数据
*/
default Page<T> paginateWithRelations(int pageNumber, int pageSize, int totalRow, QueryCondition whereConditions) {
FlexAssert.notNull(whereConditions, "whereConditions can not be null.");
FlexAssert.notNull(whereConditions, "whereConditions");
Page<T> page = new Page<>(pageNumber, pageSize, totalRow);
return paginateWithRelations(page, new QueryWrapper().where(whereConditions));
}

View File

@ -21,6 +21,7 @@ import com.mybatisflex.annotation.SetListener;
import com.mybatisflex.annotation.UpdateListener;
import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.core.dialect.DbType;
import com.mybatisflex.core.exception.FlexAssert;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
@ -307,9 +308,7 @@ public class FlexGlobalConfig {
}
public void setNormalValueOfLogicDelete(Object normalValueOfLogicDelete) {
if (normalValueOfLogicDelete == null) {
throw new NullPointerException("normalValueOfLogicDelete can not be null.");
}
FlexAssert.notNull(normalValueOfLogicDelete,"normalValueOfLogicDelete");
this.normalValueOfLogicDelete = normalValueOfLogicDelete;
}
@ -318,9 +317,7 @@ public class FlexGlobalConfig {
}
public void setDeletedValueOfLogicDelete(Object deletedValueOfLogicDelete) {
if (deletedValueOfLogicDelete == null) {
throw new NullPointerException("deletedValueOfLogicDelete can not be null.");
}
FlexAssert.notNull(deletedValueOfLogicDelete,"deletedValueOfLogicDelete");
this.deletedValueOfLogicDelete = deletedValueOfLogicDelete;
}

View File

@ -16,6 +16,7 @@
package com.mybatisflex.core;
import com.mybatisflex.core.datasource.FlexDataSource;
import com.mybatisflex.core.exception.FlexAssert;
import com.mybatisflex.core.mybatis.FlexConfiguration;
import com.mybatisflex.core.mybatis.FlexSqlSessionFactoryBuilder;
import com.mybatisflex.core.mybatis.Mappers;
@ -92,9 +93,8 @@ public class MybatisFlexBootstrap {
public MybatisFlexBootstrap start() {
if (started.compareAndSet(false, true)) {
if (dataSource == null) {
throw new IllegalStateException("dataSource can not be null.");
}
FlexAssert.notNull(dataSource,"dataSource");
//init configuration
if (configuration == null) {

View File

@ -16,6 +16,8 @@
package com.mybatisflex.core.exception;
import com.mybatisflex.core.exception.locale.LocalizedFormats;
import java.util.Collection;
import java.util.Map;
@ -23,6 +25,8 @@ import java.util.Map;
* 断言
*
* @author 王帅
* @author michael
*
* @since 2023-07-08
*/
public final class FlexAssert {
@ -34,25 +38,26 @@ public final class FlexAssert {
* 断言对象不为空如果为空抛出异常并指明哪个对象为空
*
* @param obj 对象
* @param message 错误消息
* @param param 错误消息参数
* @throws MybatisFlexException 如果对象为空抛出此异常
*/
public static void notNull(Object obj, String message) {
public static void notNull(Object obj, String param) {
if (obj == null) {
throw FlexExceptions.wrap(message);
throw FlexExceptions.wrap(LocalizedFormats.OBJECT_NULL, param);
}
}
/**
* 断言 Map 集合不为 {@code null} 或者空集合如果为空则抛出异常并指明为什么不允许为空集合
*
* @param map Map 集合
* @param message 错误消息
* @param param 错误消息参数
* @throws MybatisFlexException 如果集合为空抛出此异常
*/
public static void notEmpty(Map<?, ?> map, String message) {
public static void notEmpty(Map<?, ?> map, String param) {
if (map == null || map.isEmpty()) {
throw FlexExceptions.wrap(message);
throw FlexExceptions.wrap(LocalizedFormats.MAP_NULL_OR_EMPTY, param);
}
}
@ -60,12 +65,12 @@ public final class FlexAssert {
* 断言集合不为 {@code null} 或者空集合如果为空则抛出异常并指明为什么不允许为空集合
*
* @param collection 集合
* @param message 错误消息
* @param param 错误消息参数
* @throws MybatisFlexException 如果集合为空抛出此异常
*/
public static void notEmpty(Collection<?> collection, String message) {
public static void notEmpty(Collection<?> collection, String param) {
if (collection == null || collection.isEmpty()) {
throw FlexExceptions.wrap(message);
throw FlexExceptions.wrap(LocalizedFormats.MAP_NULL_OR_EMPTY, param);
}
}
@ -73,12 +78,12 @@ public final class FlexAssert {
* 断言数组不为 {@code null} 或者空数组如果为空则抛出异常并指明为什么不允许为空数组
*
* @param array 数组
* @param message 错误消息
* @param param 错误消息参数
* @throws MybatisFlexException 如果数组为空抛出此异常
*/
public static <T> void notEmpty(T[] array, String message) {
public static <T> void notEmpty(T[] array, String param) {
if (array == null || array.length == 0) {
throw FlexExceptions.wrap(message);
throw FlexExceptions.wrap(param);
}
}

View File

@ -34,6 +34,9 @@ public enum LocalizedFormats implements Localizable {
* object can not be null
*/
OBJECT_NULL("{0} can not be null."),
OBJECT_NULL_OR_BLANK("{0} can not be null or blank."),
MAP_NULL_OR_EMPTY("{0} can not be null or empty."),
ARRAY_NULL_OR_EMPTY("{0} array can not be null or empty."),
DATASOURCE_TYPE_BLANK("The dataSource type can not be null or blank."),
@ -45,7 +48,8 @@ public enum LocalizedFormats implements Localizable {
UPDATE_ONLY_SUPPORT_1_TABLE("\"UpdateByQuery\" only support 1 table."),
UPDATE_OR_DELETE_NOT_ALLOW("Not allowed \"UPDATE\" or \"DELETE\" a table without where condition."),
ENTITY_VERSION_NULL("The version value of entity[{0}] must not be null."),
ENTITY_VERSION_NULL("The version value of entity \"{0}\" must not be null."),
;
private final String sourceFormat;

View File

@ -42,9 +42,9 @@ public class PrimaryKeyLogicDeleteProcessor extends AbstractLogicDeleteProcessor
@Override
public String buildLogicDeletedSet(String logicColumn, TableInfo tableInfo, IDialect dialect) {
List<IdInfo> primaryKeyList = tableInfo.getPrimaryKeyList();
FlexAssert.notEmpty(primaryKeyList, "Entity must have one primary key.");
String column = primaryKeyList.get(0).getColumn();
List<IdInfo> primaryKeys = tableInfo.getPrimaryKeyList();
FlexAssert.notEmpty(primaryKeys, "primaryKeys");
String column = primaryKeys.get(0).getColumn();
return dialect.wrap(logicColumn) + EQUALS + dialect.wrap(column);
}

View File

@ -52,7 +52,7 @@ public class EntitySqlProvider {
public static String insert(Map params, ProviderContext context) {
Object entity = ProviderUtil.getEntity(params);
FlexAssert.notNull(entity, "entity can not be null.");
FlexAssert.notNull(entity, "entity");
boolean ignoreNulls = ProviderUtil.isIgnoreNulls(params);
@ -88,7 +88,7 @@ public class EntitySqlProvider {
public static String insertWithPk(Map params, ProviderContext context) {
Object entity = ProviderUtil.getEntity(params);
FlexAssert.notNull(entity, "entity can not be null.");
FlexAssert.notNull(entity, "entity");
boolean ignoreNulls = ProviderUtil.isIgnoreNulls(params);
@ -125,7 +125,7 @@ public class EntitySqlProvider {
public static String insertBatch(Map params, ProviderContext context) {
List<Object> entities = ProviderUtil.getEntities(params);
FlexAssert.notEmpty(entities, "entities can not be null or empty.");
FlexAssert.notEmpty(entities, "entities");
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
for (Object entity : entities) {
@ -160,7 +160,7 @@ public class EntitySqlProvider {
public static String deleteById(Map params, ProviderContext context) {
Object[] primaryValues = ProviderUtil.getPrimaryValues(params);
FlexAssert.notEmpty(primaryValues, "primaryValues can not be null or empty.");
FlexAssert.notEmpty(primaryValues, "primaryValues");
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
@ -182,7 +182,7 @@ public class EntitySqlProvider {
public static String deleteBatchByIds(Map params, ProviderContext context) {
Object[] primaryValues = ProviderUtil.getPrimaryValues(params);
FlexAssert.notEmpty(primaryValues, "primaryValues can not be null or empty.");
FlexAssert.notEmpty(primaryValues, "primaryValues");
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
@ -322,7 +322,7 @@ public class EntitySqlProvider {
public static String selectOneById(Map params, ProviderContext context) {
Object[] primaryValues = ProviderUtil.getPrimaryValues(params);
FlexAssert.notEmpty(primaryValues, "primaryValues can not be null or empty.");
FlexAssert.notEmpty(primaryValues, "primaryValues");
TableInfo tableInfo = ProviderUtil.getTableInfo(context);
@ -345,7 +345,7 @@ public class EntitySqlProvider {
public static String selectListByIds(Map params, ProviderContext context) {
Object[] primaryValues = ProviderUtil.getPrimaryValues(params);
FlexAssert.notEmpty(primaryValues, "primaryValues can not be null or empty.");
FlexAssert.notEmpty(primaryValues, "primaryValues");
TableInfo tableInfo = ProviderUtil.getTableInfo(context);

View File

@ -16,7 +16,9 @@
package com.mybatisflex.core.provider;
import com.mybatisflex.core.FlexConsts;
import com.mybatisflex.core.exception.FlexAssert;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.exception.locale.LocalizedFormats;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.core.table.TableInfo;
@ -55,7 +57,7 @@ class ProviderUtil {
public static String[] getPrimaryKeys(Map params) {
String primaryKey = (String) params.get(FlexConsts.PRIMARY_KEY);
if (StringUtil.isBlank(primaryKey)) {
throw FlexExceptions.wrap("primaryKey can not be null or blank.");
throw FlexExceptions.wrap(LocalizedFormats.OBJECT_NULL_OR_BLANK, "primaryKey");
}
String[] primaryKeys = primaryKey.split(",");
for (int i = 0; i < primaryKeys.length; i++) {
@ -80,9 +82,7 @@ class ProviderUtil {
public static QueryWrapper getQueryWrapper(Map params) {
Object queryWrapper = params.get(FlexConsts.QUERY);
if (queryWrapper == null) {
throw new IllegalArgumentException("queryWrapper can not be null.");
}
FlexAssert.notNull(queryWrapper,"queryWrapper");
return (QueryWrapper) queryWrapper;
}

View File

@ -79,7 +79,7 @@ public class RowSqlProvider {
public static String insertBatchWithFirstRowColumns(Map params) {
List<Row> rows = ProviderUtil.getRows(params);
FlexAssert.notEmpty(rows, "rows can not be null or empty.");
FlexAssert.notEmpty(rows, "rows");
String tableName = ProviderUtil.getTableName(params);
String schema = ProviderUtil.getSchemaName(params);
@ -109,7 +109,7 @@ public class RowSqlProvider {
public static String deleteById(Map params) {
Object[] primaryValues = ProviderUtil.getPrimaryValues(params);
FlexAssert.notEmpty(primaryValues, "primaryValue can not be null or empty.");
FlexAssert.notEmpty(primaryValues, "primaryValues");
String schema = ProviderUtil.getSchemaName(params);
String tableName = ProviderUtil.getTableName(params);
@ -211,7 +211,7 @@ public class RowSqlProvider {
public static String updateBatchById(Map params) {
List<Row> rows = ProviderUtil.getRows(params);
FlexAssert.notEmpty(rows, "rows can not be null or empty.");
FlexAssert.notEmpty(rows, "rows");
String schema = ProviderUtil.getSchemaName(params);
String tableName = ProviderUtil.getTableName(params);

View File

@ -1,8 +1,14 @@
OBJECT_NULL={0} \u4E0D\u80FD\u4E3A null \u503C\u3002
OBJECT_NULL_OR_BLANK={0} \u4e0d\u80fd\u4e3a null \u503c\u6216\u8005\u7a7a\u5b57\u7b26\u4e32\u3002
MAP_NULL_OR_EMPTY={0} \u4e0d\u80fd\u4e3a null \u503c\u6216\u8005\u7a7a\u5143\u7d20\u3002
ARRAY_NULL_OR_EMPTY={0} \u6570\u7ec4\u4e0d\u80fd\u4e3a null \u503c\u6216\u8005\u7a7a\u5143\u7d20\u3002
DATASOURCE_TYPE_BLANK=\u6570\u636e\u6e90\u7684\u7c7b\u578b\u4e0d\u80fd\u4e3a null \u6216\u8005\u7a7a\u5b57\u7b26\u4e32\u3002
DATASOURCE_TYPE_NOT_FIND=\u65e0\u6cd5\u627e\u5230\u6570\u636e\u6e90\u7c7b\u578b\uff1a {0}
DATASOURCE_CAN_NOT_INSTANCE=\u65e0\u6cd5\u6839\u636e\u7c7b\uff1a {0} \u521b\u5efa\u6570\u636e\u6e90\u3002
DATASOURCE_JDBC_URL=\u65e0\u6cd5\u83b7\u53d6\u6570\u636e\u6e90\u7c7b\u7684 jdbcUrl \u914d\u7f6e\u3002
UPDATE_ONLY_SUPPORT_1_TABLE=\"UpdateByQuery\" \u4ec5\u652f\u6301\u4f20\u5165 1 \u5f20\u8868\u3002
UPDATE_OR_DELETE_NOT_ALLOW=\u6267\u884c "update" \u6216\u8005 "delete" \u7684 SQL \u65f6\uff0c\u4e0d\u5141\u8bb8\u5168\u8868\u64cd\u4f5c\uff0c\u5fc5\u987b\u8981\u6709 where \u6761\u4ef6\u3002
ENTITY_VERSION_NULL=\u4e50\u89c2\u9501\u5b9e\u4f53\u7c7b\u5fc5\u987b\u8bbe\u7f6e version \u7684\u503c\uff1a{0}\u3002