-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathCommonException.java
More file actions
executable file
·222 lines (179 loc) · 6.22 KB
/
CommonException.java
File metadata and controls
executable file
·222 lines (179 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson.orm.exception;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.concurrent.TimeoutException;
import apijson.JSONResponse;
import apijson.Log;
import apijson.StringUtil;
import apijson.orm.AbstractSQLConfig;
import apijson.orm.SQLConfig;
/**异常包装器,主要用来包装构造器没有 Throwable 参数的 Exception
* @author Lemon
*/
public class CommonException extends Exception {
private static final long serialVersionUID = 1L;
private Integer code;
private String environment;
public CommonException setCode(Integer code) {
this.code = code;
return this;
}
public Integer getCode() {
if (code == null) {
code = getCode(getCause());
}
return code;
}
public static int getCode(Throwable e) {
boolean isCommon = Log.DEBUG && e instanceof CommonException;
Integer code = isCommon ? ((CommonException) e).getCode() : null;
if (code != null) {
return code;
}
Throwable t = isCommon ? e.getCause() : e;
if (t == null) {
return JSONResponse.CODE_SERVER_ERROR;
}
if (t instanceof UnsupportedEncodingException) {
return JSONResponse.CODE_UNSUPPORTED_ENCODING;
}
if (t instanceof IllegalAccessException) {
return JSONResponse.CODE_ILLEGAL_ACCESS;
}
if (t instanceof UnsupportedOperationException) {
return JSONResponse.CODE_UNSUPPORTED_OPERATION;
}
if (t instanceof NotExistException) {
return JSONResponse.CODE_NOT_FOUND;
}
if (t instanceof IllegalArgumentException) {
return JSONResponse.CODE_ILLEGAL_ARGUMENT;
}
if (t instanceof NotLoggedInException) {
return JSONResponse.CODE_NOT_LOGGED_IN;
}
if (t instanceof TimeoutException) {
return JSONResponse.CODE_TIME_OUT;
}
if (t instanceof ConflictException) {
return JSONResponse.CODE_CONFLICT;
}
if (t instanceof ConditionErrorException) {
return JSONResponse.CODE_CONDITION_ERROR;
}
if (t instanceof UnsupportedDataTypeException) {
return JSONResponse.CODE_UNSUPPORTED_TYPE;
}
if (t instanceof OutOfRangeException) {
return JSONResponse.CODE_OUT_OF_RANGE;
}
if (t instanceof NullPointerException) {
return JSONResponse.CODE_NULL_POINTER;
}
return JSONResponse.CODE_SERVER_ERROR;
}
public static String getMsg(Throwable e) {
if (e == null) {
return null;
}
String msg = e.getMessage();
if (msg != null) {
return msg;
}
Throwable t = e.getCause();
return t == null ? null : t.getMessage();
}
public CommonException setEnvironment(String environment) {
this.environment = environment;
return this;
}
public String getEnvironment() {
return environment;
}
public CommonException(Throwable t) {
this(null, t);
}
public CommonException(String msg, Throwable t) {
super(msg == null && t != null ? t.getMessage() : null, t);
}
public CommonException(int code, String msg) {
this(code, msg, null);
}
public CommonException(int code, String msg, Throwable t) {
this(msg, t);
setCode(code);
}
public CommonException(Throwable t, String environment) {
this(null, t);
setEnvironment(environment);
}
public static Exception wrap(Exception e, SQLConfig config) {
if (Log.DEBUG == false && e instanceof SQLException) {
return new SQLException("数据库驱动执行异常SQLException,非 Log.DEBUG 模式下不显示详情,避免泄漏真实模式名、表名等隐私信息", e);
}
// String msg = e.getMessage();
if (Log.DEBUG && (e instanceof CommonException == false || ((CommonException) e).getEnvironment() == null)) {
// msg != null && msg.contains(Log.KEY_SYSTEM_INFO_DIVIDER) == false) {
try {
String db = config == null ? AbstractSQLConfig.DEFAULT_DATABASE : (config instanceof AbstractSQLConfig
? ((AbstractSQLConfig) config).getSQLDatabase() : config.getDatabase()
);
String dbVersion = config.getDBVersion();
if (StringUtil.isEmpty(dbVersion)) {
dbVersion = "<!-- 请填写版本号,例如 8.0 -->";
}
if (db != null) {
db += " " + dbVersion;
}
else if (config.isMySQL()) {
db = SQLConfig.DATABASE_MYSQL + " " + dbVersion;
}
else if (config.isPostgreSQL()) {
db = SQLConfig.DATABASE_POSTGRESQL + " " + dbVersion;
}
else if (config.isSQLServer()) {
db = SQLConfig.DATABASE_SQLSERVER + " " + dbVersion;
}
else if (config.isOracle()) {
db = SQLConfig.DATABASE_ORACLE + " " + dbVersion;
}
else if (config.isDb2()) {
db = SQLConfig.DATABASE_DB2 + " " + dbVersion;
}
else if (config.isDameng()) {
db = SQLConfig.DATABASE_DAMENG + " " + dbVersion;
}
else if (config.isClickHouse()) {
db = SQLConfig.DATABASE_CLICKHOUSE + " " + dbVersion;
}
else if (config.isTDengine()) {
db = SQLConfig.DATABASE_TDENGINE + " " + dbVersion;
}
else {
db = "<!-- 请填写,例如 MySQL 5.7。获取到的默认数据库为 " + AbstractSQLConfig.DEFAULT_DATABASE + " -->";
}
// Class<? extends Exception> clazz = e.getClass();
// msg = msg
// + " " + Log.KEY_SYSTEM_INFO_DIVIDER + " **环境信息** "
String env = " **环境信息** "
+ " \n 系统: " + Log.OS_NAME + " " + Log.OS_VERSION
+ " \n 数据库: " + db
+ " \n JDK: " + Log.JAVA_VERSION + " " + Log.OS_ARCH
+ " \n APIJSON: " + Log.VERSION;
if (e instanceof CommonException) {
((CommonException) e).setEnvironment(env);
return e;
}
// try {
// e = clazz.getConstructor(String.class, Throwable.class).newInstance(msg, e);
// }
// catch (Throwable e2) {
return new CommonException(e, env); // e = clazz.getConstructor(String.class).newInstance(msg);
// }
} catch (Throwable e2) {}
}
return e;
}
}