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

This commit is contained in:
开源海哥 2023-07-27 16:01:23 +08:00
parent 90ff895125
commit 5dc95c5567
7 changed files with 48 additions and 9 deletions

View File

@ -16,6 +16,7 @@
package com.mybatisflex.core.datasource;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.exception.locale.LocalizedFormats;
import com.mybatisflex.core.util.ConvertUtil;
import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.reflection.Reflector;
@ -57,9 +58,9 @@ public class DataSourceBuilder {
if (StringUtil.isBlank(dataSourceClassName)) {
if (StringUtil.isBlank(type)) {
throw FlexExceptions.wrap("The dataSource type can not be null or blank.");
throw FlexExceptions.wrap(LocalizedFormats.DATASOURCE_TYPE_BLANK);
} else {
throw FlexExceptions.wrap("Cannot find the dataSource type: " + type);
throw FlexExceptions.wrap(LocalizedFormats.DATASOURCE_TYPE_NOT_FIND, type);
}
}
@ -69,7 +70,7 @@ public class DataSourceBuilder {
setDataSourceProperties(dataSourceObject);
return (DataSource) dataSourceObject;
} catch (Exception e) {
throw new RuntimeException("Cannot new instance dataSource by class: " + dataSourceClassName);
throw FlexExceptions.wrap(LocalizedFormats.DATASOURCE_CAN_NOT_INSTANCE, dataSourceClassName);
}
}

View File

@ -17,6 +17,7 @@ package com.mybatisflex.core.dialect;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.exception.locale.LocalizedFormats;
import com.mybatisflex.core.util.StringUtil;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
@ -70,7 +71,7 @@ public class DbTypeUtil {
connection = dataSource.getConnection();
return connection.getMetaData().getURL();
} catch (Exception e) {
throw FlexExceptions.wrap("Can not get the dataSource jdbcUrl", e);
throw FlexExceptions.wrap(e, LocalizedFormats.DATASOURCE_JDBC_URL);
} finally {
if (connection != null) {
try {

View File

@ -19,6 +19,7 @@ import com.mybatisflex.core.dialect.IDialect;
import com.mybatisflex.core.dialect.KeywordWrap;
import com.mybatisflex.core.dialect.LimitOffsetProcessor;
import com.mybatisflex.core.exception.FlexExceptions;
import com.mybatisflex.core.exception.locale.LocalizedFormats;
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
import com.mybatisflex.core.query.*;
import com.mybatisflex.core.row.Row;
@ -248,7 +249,7 @@ public class CommonsDialectImpl implements IDialect {
List<QueryTable> queryTables = CPI.getQueryTables(queryWrapper);
if (queryTables == null || queryTables.size() != 1) {
throw FlexExceptions.wrap("update sql must need 1 table.");
throw FlexExceptions.wrap(LocalizedFormats.UPDATE_ONLY_SUPPORT_1_TABLE);
}
//fix: support schema
@ -765,7 +766,7 @@ public class CommonsDialectImpl implements IDialect {
if (StringUtil.isNotBlank(versionColumn)) {
Object versionValue = tableInfo.buildColumnSqlArg(entity, versionColumn);
if (versionValue == null) {
throw FlexExceptions.wrap("The version value of entity[%s] must not be null.", entity);
throw FlexExceptions.wrap(LocalizedFormats.ENTITY_VERSION_NULL, entity);
}
sql.append(AND).append(wrap(versionColumn)).append(EQUALS).append(versionValue);
}

View File

@ -63,6 +63,10 @@ public final class FlexExceptions {
return new MybatisFlexException(String.format(msg, params));
}
public static MybatisFlexException wrap(Throwable cause, Localizable pattern, Object... args) {
return new MybatisFlexException(cause, pattern, args);
}
public static MybatisFlexException wrap(Localizable pattern, Object... args) {
return new MybatisFlexException(pattern, args);
}

View File

@ -20,6 +20,10 @@ import com.mybatisflex.core.exception.locale.Localizable;
import java.text.MessageFormat;
import java.util.Locale;
/**
* @author michael
* @author 王帅
*/
public class MybatisFlexException extends RuntimeException {
private static final long serialVersionUID = 1L;
@ -27,6 +31,12 @@ public class MybatisFlexException extends RuntimeException {
private Localizable pattern;
private Object[] arguments;
public MybatisFlexException(Throwable cause, Localizable pattern, Object[] arguments) {
super(cause);
this.pattern = pattern;
this.arguments = arguments;
}
public MybatisFlexException(Localizable pattern, Object... arguments) {
this.pattern = pattern;
this.arguments = arguments;

View File

@ -24,11 +24,28 @@ import java.util.ResourceBundle;
* 异常消息中使用的本地化消息格式的枚举
*
* @author 王帅
* @author michael
*
* @since 2023-07-26
*/
public enum LocalizedFormats implements Localizable {
OBJECT_NULL("{0} can not be null.");
/**
* object can not be null
*/
OBJECT_NULL("{0} can not be null."),
DATASOURCE_TYPE_BLANK("The dataSource type can not be null or blank."),
DATASOURCE_TYPE_NOT_FIND("Can not find the dataSource type: {0}"),
DATASOURCE_CAN_NOT_INSTANCE("Can not new instance dataSource object by class: {0}"),
DATASOURCE_JDBC_URL("Can not get the dataSource jdbcUrl."),
UPDATE_ONLY_SUPPORT_1_TABLE("\"UpdateByQuery\" only support 1 table."),
ENTITY_VERSION_NULL("The version value of entity[{0}] must not be null."),
;
private final String sourceFormat;
@ -50,10 +67,9 @@ public enum LocalizedFormats implements Localizable {
ResourceBundle bundle = ResourceBundle.getBundle("assets/" + path, locale);
// 获取当前语言的消息格式
if (bundle.getLocale().getLanguage().equals(locale.getLanguage())) {
return bundle.getString(toString());
return bundle.getString(name());
}
} catch (MissingResourceException mre) {
mre.printStackTrace();
// do nothing here.
}
// 如果没有该语言的本地化消息则返回源消息字符串

View File

@ -1 +1,7 @@
OBJECT_NULL={0} \u4E0D\u80FD\u4E3A null \u503C\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
ENTITY_VERSION_NULL=\u4e50\u89c2\u9501\u5b9e\u4f53\u7c7b\u5fc5\u987b\u8bbe\u7f6e version \u7684\u503c\uff1a{0}\u3002