Skip to content

Commit 5f78abf

Browse files
committed
[new] 更换异常写法
1 parent 1475a5c commit 5f78abf

File tree

18 files changed

+162
-122
lines changed

18 files changed

+162
-122
lines changed

common-util/src/main/java/com/vonchange/common/util/MarkdownUtil.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33

44
import com.vonchange.common.util.exception.EnumUtilErrorCode;
5+
import com.vonchange.common.util.exception.ErrorMsg;
56
import com.vonchange.common.util.exception.UtilException;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
@@ -32,7 +33,8 @@ public static synchronized Map<String,String> readMarkdownFile(String id,b
3233
if(!notFoundError){
3334
return null;
3435
}
35-
throw new UtilException(path+" not Find");
36+
throw new UtilException(EnumUtilErrorCode.MarkdownPathNotFound,
37+
ErrorMsg.builder().message(path+" not found"));
3638
}
3739
String content=null;
3840
try {
@@ -151,7 +153,8 @@ public static String getContent(String id,boolean notFoundThrowError) {
151153
if(!notFoundThrowError){
152154
return null;
153155
}
154-
throw new UtilException(EnumUtilErrorCode.MarkdownIdNotFound,id+" not found");
156+
throw new UtilException(EnumUtilErrorCode.MarkdownIdNotFound,
157+
ErrorMsg.builder().message(id+" not found"));
155158
}
156159
String filePath= UtilAll.UString.substringBeforeLast(id, StringPool.DOT);
157160
String codeId= id.substring(filePath.length()+1);
@@ -160,7 +163,8 @@ public static String getContent(String id,boolean notFoundThrowError) {
160163
if(!notFoundThrowError){
161164
return null;
162165
}
163-
throw new UtilException(EnumUtilErrorCode.MarkdownIdNotFound,id+" not found");
166+
throw new UtilException(EnumUtilErrorCode.MarkdownIdNotFound,
167+
ErrorMsg.builder().message(id+" not found"));
164168
}
165169
String content= extendContent(contentMap,codeId);
166170
if(UtilAll.UString.isNotBlank(content)){

common-util/src/main/java/com/vonchange/common/util/UtilAll.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,9 @@ public static boolean isNotBlank(final CharSequence cs) {
308308
}
309309

310310
public static String format(String messageTemplate, Object... parameters) {
311+
if(null==parameters||parameters.length==0){
312+
return messageTemplate;
313+
}
311314
return MessageFormatter.arrayFormat(messageTemplate, parameters).getMessage();
312315
}
313316
public static String formatException(String messageTemplate,Throwable throwable, Object... parameters) {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
package com.vonchange.common.util.exception;
3+
4+
5+
public class CommonRuntimeException extends RuntimeException {
6+
7+
8+
private static final long serialVersionUID = 1050921059666314224L;
9+
private final Object errorCode;
10+
11+
public CommonRuntimeException(Object errorCode) {
12+
super(errorCode.toString());
13+
this.errorCode = errorCode;
14+
}
15+
16+
public CommonRuntimeException(Object errorCode, ErrorMsg errorMsg) {
17+
super(errorCode.toString() + " " + errorMsg.getMessage(), errorMsg.getThrowable());
18+
this.errorCode = errorCode;
19+
}
20+
21+
22+
public Object getErrorCode() {
23+
return errorCode;
24+
}
25+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.vonchange.common.util.exception;
22

33
public enum EnumUtilErrorCode {
4-
Error,NullValue,MarkdownIdNotFound
4+
Error,NullValue,MarkdownIdNotFound,MarkdownPathNotFound
55
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.vonchange.common.util.exception;
2+
3+
import com.vonchange.common.util.UtilAll;
4+
5+
public class ErrorMsg{
6+
private String message;
7+
private Throwable throwable;
8+
9+
10+
public static ErrorMsg builder(){
11+
return new ErrorMsg();
12+
}
13+
public ErrorMsg message(String message) {
14+
this.message =message;
15+
return this;
16+
}
17+
public ErrorMsg message(String message, Object... parameters) {
18+
this.message = UtilAll.UString.format(message,parameters);
19+
return this;
20+
}
21+
public ErrorMsg throwable( Throwable throwable) {
22+
this.throwable =throwable;
23+
return this;
24+
}
25+
26+
27+
public String getMessage() {
28+
return message;
29+
}
30+
31+
32+
public Throwable getThrowable() {
33+
return throwable;
34+
}
35+
}

common-util/src/main/java/com/vonchange/common/util/exception/UtilException.java

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,16 @@
22
package com.vonchange.common.util.exception;
33

44

5-
public class UtilException extends RuntimeException {
5+
public class UtilException extends CommonRuntimeException {
6+
private static final long serialVersionUID = -8919479339352899374L;
67

7-
private static final long serialVersionUID = 1L;
8-
protected EnumUtilErrorCode code = EnumUtilErrorCode.Error;
9-
10-
public UtilException() {
11-
super();
12-
}
13-
14-
public UtilException(String message, Throwable cause) {
15-
super(message, cause);
16-
}
17-
18-
public UtilException(String message) {
19-
super(message);
20-
}
21-
22-
public UtilException(EnumUtilErrorCode code,String message) {
23-
super(message);
24-
this.code = code;
25-
}
268
public UtilException(EnumUtilErrorCode code) {
27-
super(code.name());
28-
this.code = code;
9+
super(code);
2910
}
30-
31-
public UtilException(Throwable cause) {
32-
super(cause);
11+
public UtilException(EnumUtilErrorCode code,ErrorMsg message) {
12+
super(code,message);
3313
}
34-
35-
36-
public void setCode(EnumUtilErrorCode code) {
37-
this.code = code;
14+
public EnumUtilErrorCode getErrorCode() {
15+
return (EnumUtilErrorCode) super.getErrorCode();
3816
}
39-
40-
41-
public EnumUtilErrorCode getCode() {
42-
return code;
43-
}
44-
4517
}

common-util/src/test/java/MarkdownUtilTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ public class MarkdownUtilTest {
66

77
@Test
88
public void markdownUtilTest(){
9-
String content = MarkdownUtil.getContent("config.test.sql");
9+
String content = MarkdownUtil.getContent("config.test.sql2");
1010
System.out.println(content);
11+
1112
}
1213
@Test
1314
public void markdownUtilTest2(){

jdbc-mybatis/src/main/java/com/vonchange/jdbc/client/DefaultJdbcClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.vonchange.jdbc.client;
22

33
import com.vonchange.common.util.StringPool;
4+
import com.vonchange.common.util.exception.ErrorMsg;
45
import com.vonchange.jdbc.config.EnumRWType;
56
import com.vonchange.jdbc.config.EnumSqlRead;
67
import com.vonchange.jdbc.core.CrudUtil;
@@ -11,6 +12,7 @@
1112
import com.vonchange.jdbc.model.DataSourceWrapper;
1213
import com.vonchange.jdbc.model.SqlParam;
1314
import com.vonchange.jdbc.util.MybatisTpl;
15+
import com.vonchange.mybatis.exception.EnumJdbcErrorCode;
1416
import com.vonchange.mybatis.exception.JdbcMybatisRuntimeException;
1517
import org.slf4j.Logger;
1618
import org.slf4j.LoggerFactory;
@@ -87,7 +89,8 @@ public <T> MappedQuerySpec<T> query(Class<T> mappedClass){
8789
}
8890
private <T> SqlParam getSqlParameter(String sql){
8991
if(!sql.contains(StringPool.SPACE)){
90-
throw new JdbcMybatisRuntimeException("{} error",sql);
92+
throw new JdbcMybatisRuntimeException(EnumJdbcErrorCode.SqlError,
93+
ErrorMsg.builder().message("{} error",sql));
9194
}
9295
if(sql.contains("[@")||sql.contains("#{")){
9396
SqlParam sqlParam = MybatisTpl.generate("mybatis_sql",sql,namedParams,dataSourceWrapper.getDialect());

jdbc-mybatis/src/main/java/com/vonchange/jdbc/core/CrudUtil.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.vonchange.common.util.StringPool;
66
import com.vonchange.common.util.UtilAll;
77
import com.vonchange.common.util.bean.BeanUtil;
8+
import com.vonchange.common.util.exception.ErrorMsg;
89
import com.vonchange.jdbc.config.ConstantJdbc;
910
import com.vonchange.jdbc.config.EnumSqlRead;
1011
import com.vonchange.jdbc.count.CountSqlParser;
@@ -18,6 +19,7 @@
1819
import com.vonchange.jdbc.util.NameQueryUtil;
1920
import com.vonchange.jdbc.util.OrmUtil;
2021
import com.vonchange.mybatis.dialect.Dialect;
22+
import com.vonchange.mybatis.exception.EnumJdbcErrorCode;
2123
import com.vonchange.mybatis.exception.JdbcMybatisRuntimeException;
2224
import org.slf4j.Logger;
2325
import org.slf4j.LoggerFactory;
@@ -54,11 +56,13 @@ public static <T> SqlParam generateUpdateSql(T entity, boolean isNullUpdate, bo
5456
String tableName = entityInfo.getTableName();
5557
String idColumnName = entityInfo.getIdColumnName();
5658
if (null == idColumnName) {
57-
throw new JdbcMybatisRuntimeException("need entity field @ID");
59+
throw new JdbcMybatisRuntimeException(EnumJdbcErrorCode.NeedIdAnnotation,
60+
ErrorMsg.builder().message("need entity field @ID"));
5861
}
5962
Object idValue = BeanUtil.getPropertyT(entity, entityInfo.getIdFieldName());
6063
if (null == idValue) {
61-
throw new JdbcMybatisRuntimeException("ID value is null,can not update");
64+
throw new JdbcMybatisRuntimeException(EnumJdbcErrorCode.ParamEmpty,
65+
ErrorMsg.builder().message("ID value is null,can not update"));
6266
}
6367
List<EntityField> entityFieldList = entityInfo.getEntityFields();
6468
List<String> columns = new ArrayList<>();
@@ -127,7 +131,8 @@ public static Two<String,Collection<?>> conditionSql(QueryColumn queryColumn){
127131
}
128132
private static Two<String,Collection<?>> inSql(String sql, Object value){
129133
if (!(value instanceof Collection ||value.getClass().isArray())){
130-
throw new JdbcMybatisRuntimeException("in query parameter must collection or array");
134+
throw new JdbcMybatisRuntimeException(EnumJdbcErrorCode.MustArray,
135+
ErrorMsg.builder().message("in query parameter must collection or array"));
131136
}
132137
StringBuilder inSb=new StringBuilder(sql+" (");
133138
Collection<Object> collection = new ArrayList<>();
@@ -148,7 +153,8 @@ private static Two<String,Collection<?>> inSql(String sql, Object value){
148153
}
149154
private static Two<String,Collection<?>> betweenSql(String sql, Object value) {
150155
if (!(value instanceof Collection||value.getClass().isArray())){
151-
throw new JdbcMybatisRuntimeException("between query parameter must collection or array");
156+
throw new JdbcMybatisRuntimeException(EnumJdbcErrorCode.MustArray,
157+
ErrorMsg.builder().message("between query parameter must collection or array"));
152158
}
153159
String betweenSql=sql+" ? and ? ";
154160
Collection<Object> collection = new ArrayList<>();
@@ -164,7 +170,8 @@ private static Two<String,Collection<?>> betweenSql(String sql, Object value) {
164170
}
165171
}
166172
if(collection.size()>2||collection.size()<1){
167-
throw new JdbcMybatisRuntimeException("between query param error");
173+
throw new JdbcMybatisRuntimeException(EnumJdbcErrorCode.Error,
174+
ErrorMsg.builder().message("between query param error"));
168175
}
169176
return Two.of(betweenSql,collection);
170177
}

jdbc-mybatis/src/main/java/com/vonchange/jdbc/mapper/BeanInsertMapper.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.vonchange.jdbc.mapper;
22

33
import com.vonchange.common.util.UtilAll;
4+
import com.vonchange.common.util.exception.ErrorMsg;
45
import com.vonchange.jdbc.core.CrudUtil;
6+
import com.vonchange.jdbc.model.EntityInfo;
57
import com.vonchange.jdbc.util.ConvertMap;
6-
import com.vonchange.mybatis.exception.JdbcMybatisRuntimeException;
78
import com.vonchange.jdbc.util.EntityUtil;
8-
import com.vonchange.jdbc.model.EntityInfo;
9+
import com.vonchange.mybatis.exception.EnumJdbcErrorCode;
10+
import com.vonchange.mybatis.exception.JdbcMybatisRuntimeException;
911
import org.springframework.jdbc.core.ResultSetExtractor;
1012

1113
import java.sql.ResultSet;
@@ -39,7 +41,8 @@ private void toBean(ResultSet rs, T entity) throws SQLException {
3941
EntityInfo entityInfo = EntityUtil.getEntityInfo(entity.getClass());
4042
String idFieldName = entityInfo.getIdFieldName();
4143
if(UtilAll.UString.isBlank(idFieldName)){
42-
throw new JdbcMybatisRuntimeException("need @Id in your entity");
44+
throw new JdbcMybatisRuntimeException(EnumJdbcErrorCode.NeedIdAnnotation,
45+
ErrorMsg.builder().message("need @Id in your entity"));
4346
}
4447
ConvertMap.toBean(CrudUtil.rowToMap(entityInfo,rs,idFieldName),entity);
4548
}

0 commit comments

Comments
 (0)