Repository navigation
Expand file tree
/
Copy pathCommonException.java
More file actions
executable file
·317 lines (273 loc) · 9.69 KB
/
Copy pathCommonException.java
File metadata and controls
executable file
·317 lines (273 loc) · 9.69 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*Copyright (C) 2026 the APIJSON group. 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.net.HttpRetryException;
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 HttpRetryException) {
return ((HttpRetryException) t).responseCode();
}
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).gainSQLDatabase() : config.getDatabase()
);
String dbVersion = config == null ? null : config.gainDBVersion();
if (StringUtil.isEmpty(dbVersion)) {
dbVersion = "<!-- 请填写版本号,例如 8.0 -->";
}
if (db != null || config == null) {
db += " " + dbVersion;
}
else if (config.isMySQL()) {
db = SQLConfig.DATABASE_MYSQL + " " + dbVersion;
}
else if (config.isMariaDB()) {
db = SQLConfig.DATABASE_MARIADB + " " + dbVersion;
}
else if (config.isTiDB()) {
db = SQLConfig.DATABASE_TIDB + " " + dbVersion;
}
else if (config.isPostgreSQL()) {
db = SQLConfig.DATABASE_POSTGRESQL + " " + dbVersion;
}
else if (config.isCockroachDB()) {
db = SQLConfig.DATABASE_COCKROACHDB + " " + 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.isDuckDB()) {
db = SQLConfig.DATABASE_DUCKDB + " " + dbVersion;
}
else if (config.isSurrealDB()) {
db = SQLConfig.DATABASE_SURREALDB + " " + dbVersion;
}
else if (config.isOpenGauss()) {
db = SQLConfig.DATABASE_OPENGAUSS + " " + dbVersion;
}
else if (config.isDameng()) {
db = SQLConfig.DATABASE_DAMENG + " " + dbVersion;
}
else if (config.isKingBaseMySQL()) {
db = SQLConfig.DATABASE_KINGBASE_MYSQL + " " + dbVersion;
}
else if (config.isKingBaseOracle()) {
db = SQLConfig.DATABASE_KINGBASE_ORACLE + " " + dbVersion;
}
else if (config.isKingBaseSQLServer()) {
db = SQLConfig.DATABASE_KINGBASE_SQLSERVER + " " + dbVersion;
}
else if (config.isKingBase()) {
db = SQLConfig.DATABASE_KINGBASE + " " + dbVersion;
}
else if (config.isElasticsearch()) {
db = SQLConfig.DATABASE_ELASTICSEARCH + " " + dbVersion;
}
else if (config.isManticore()) {
db = SQLConfig.DATABASE_MANTICORE + " " + dbVersion;
}
else if (config.isClickHouse()) {
db = SQLConfig.DATABASE_CLICKHOUSE + " " + dbVersion;
}
else if (config.isMilvus()) {
db = SQLConfig.DATABASE_MILVUS + " " + dbVersion;
}
else if (config.isInfluxDB()) {
db = SQLConfig.DATABASE_INFLUXDB + " " + dbVersion;
}
else if (config.isTDengine()) {
db = SQLConfig.DATABASE_TDENGINE + " " + dbVersion;
}
else if (config.isTimescaleDB()) {
db = SQLConfig.DATABASE_TIMESCALEDB + " " + dbVersion;
}
else if (config.isQuestDB()) {
db = SQLConfig.DATABASE_QUESTDB + " " + dbVersion;
}
else if (config.isIoTDB()) {
db = SQLConfig.DATABASE_IOTDB + " " + dbVersion;
}
else if (config.isSQLite()) {
db = SQLConfig.DATABASE_SQLITE + " " + dbVersion;
}
else if (config.isHive()) {
db = SQLConfig.DATABASE_HIVE + " " + dbVersion;
}
else if (config.isPresto()) {
db = SQLConfig.DATABASE_PRESTO + " " + dbVersion;
}
else if (config.isTrino()) {
db = SQLConfig.DATABASE_TRINO + " " + dbVersion;
}
else if (config.isDoris()) {
db = SQLConfig.DATABASE_DORIS + " " + dbVersion;
}
else if (config.isStarRocks()) {
db = SQLConfig.DATABASE_STARROCKS + " " + dbVersion;
}
else if (config.isSnowflake()) {
db = SQLConfig.DATABASE_SNOWFLAKE + " " + dbVersion;
}
else if (config.isDatabend()) {
db = SQLConfig.DATABASE_DATABEND + " " + dbVersion;
}
else if (config.isDatabricks()) {
db = SQLConfig.DATABASE_DATABRICKS + " " + dbVersion;
}
else if (config.isMongoDB()) {
db = SQLConfig.DATABASE_MONGODB + " " + dbVersion;
}
else if (config.isCassandra()) {
db = SQLConfig.DATABASE_CASSANDRA + " " + dbVersion;
}
else if (config.isRedis()) {
db = SQLConfig.DATABASE_REDIS + " " + dbVersion;
}
else if (config.isKafka()) {
db = SQLConfig.DATABASE_KAFKA + " " + 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;
}
}