Skip to content

Commit 914e22d

Browse files
committed
报错信息新增搜索链接及带系统信息的提交问题模板
1 parent 147cb7a commit 914e22d

File tree

3 files changed

+111
-8
lines changed

3 files changed

+111
-8
lines changed

APIJSONORM/src/main/java/apijson/Log.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
public class Log {
1414

1515
public static boolean DEBUG = true;
16+
17+
public static final String VERSION = "4.8.0";
18+
public static final String KEY_SYSTEM_INFO_DIVIDER = "---|-----APIJSON SYSTEM INFO-----|---";
1619

1720
//默认的时间格式
1821
public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");

APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public AbstractObjectParser(@NotNull JSONObject request, String parentPath, SQLC
116116
public String getParentPath() {
117117
return parentPath;
118118
}
119-
119+
120120
@Override
121121
public AbstractObjectParser setParentPath(String parentPath) {
122122
this.parentPath = parentPath;
@@ -284,6 +284,42 @@ else if (method == PUT && value instanceof JSONArray && (whereList == null || wh
284284
}
285285
} catch (Exception e) {
286286
if (tri == false) {
287+
if (Log.DEBUG && sqlConfig != null && e.getMessage().contains(Log.KEY_SYSTEM_INFO_DIVIDER) == false) {
288+
try {
289+
String db = sqlConfig.getDatabase();
290+
if (db == null) {
291+
if (sqlConfig.isPostgreSQL()) {
292+
db = SQLConfig.DATABASE_POSTGRESQL;
293+
}
294+
else if (sqlConfig.isSQLServer()) {
295+
db = SQLConfig.DATABASE_SQLSERVER;
296+
}
297+
else if (sqlConfig.isOracle()) {
298+
db = SQLConfig.DATABASE_ORACLE;
299+
}
300+
else if (sqlConfig.isDb2()) {
301+
db = SQLConfig.DATABASE_DB2;
302+
}
303+
else if (sqlConfig.isClickHouse()) {
304+
db = SQLConfig.DATABASE_CLICKHOUSE;
305+
}
306+
else {
307+
db = SQLConfig.DATABASE_MYSQL;
308+
}
309+
}
310+
311+
Class<? extends Exception> clazz = e.getClass();
312+
e = clazz.getConstructor(String.class).newInstance(
313+
e.getMessage()
314+
+ " " + Log.KEY_SYSTEM_INFO_DIVIDER + " \n**环境信息** "
315+
+ "\n系统: " + System.getProperty("os.name") + " " + System.getProperty("os.version")
316+
+ "\nJDK: " + System.getProperty("java.version") + " " + System.getProperty("os.arch")
317+
+ "\n数据库: " + db + " " + sqlConfig.getDBVersion()
318+
+ "\nAPIJSON: " + Log.VERSION
319+
);
320+
} catch (Throwable e2) {}
321+
}
322+
287323
throw e; // 不忽略错误,抛异常
288324
}
289325
invalidate(); // 忽略错误,还原request
@@ -860,30 +896,30 @@ public JSONObject onSQLExecute() throws Exception {
860896

861897
boolean isSimpleArray = false;
862898
List<JSONObject> rawList = null;
863-
899+
864900
if (isArrayMainTable && position == 0 && result != null) {
865-
901+
866902
isSimpleArray = (functionMap == null || functionMap.isEmpty())
867903
&& (customMap == null || customMap.isEmpty())
868904
&& (childMap == null || childMap.isEmpty())
869905
&& (table.equals(arrayTable));
870-
906+
871907
// 提取并缓存数组主表的列表数据
872908
rawList = (List<JSONObject>) result.remove(SQLExecutor.KEY_RAW_LIST);
873909
if (rawList != null) {
874910
String arrayPath = parentPath.substring(0, parentPath.lastIndexOf("[]") + 2);
875911

876912
if (isSimpleArray == false) {
877913
long startTime = System.currentTimeMillis();
878-
914+
879915
for (int i = 1; i < rawList.size(); i++) { // 从 1 开始,0 已经处理过
880916
JSONObject obj = rawList.get(i);
881917

882918
if (obj != null) {
883919
parser.putQueryResult(arrayPath + "/" + i + "/" + name, obj); // 解决获取关联数据时requestObject里不存在需要的关联数据
884920
}
885921
}
886-
922+
887923
long endTime = System.currentTimeMillis(); // 3ms - 8ms
888924
Log.e(TAG, "\n onSQLExecute <<<<<<<<<<<<<<<<<<<<<<<<<<<<\n for (int i = 1; i < list.size(); i++) startTime = " + startTime
889925
+ "; endTime = " + endTime + "; duration = " + (endTime - startTime) + "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n ");
@@ -895,7 +931,7 @@ public JSONObject onSQLExecute() throws Exception {
895931

896932
if (isSubquery == false && result != null) {
897933
parser.putQueryResult(path, result); // 解决获取关联数据时requestObject里不存在需要的关联数据
898-
934+
899935
if (isSimpleArray && rawList != null) {
900936
result.put(SQLExecutor.KEY_RAW_LIST, rawList);
901937
}

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static apijson.RequestMethod.GET;
1010

1111
import java.io.UnsupportedEncodingException;
12+
import java.net.URLEncoder;
1213
import java.sql.Connection;
1314
import java.sql.SQLException;
1415
import java.sql.Savepoint;
@@ -641,8 +642,35 @@ public static JSONObject newSuccessResult() {
641642
* @return
642643
*/
643644
public static JSONObject extendErrorResult(JSONObject object, Exception e) {
645+
String msg = e.getMessage();
646+
647+
if (Log.DEBUG) {
648+
try {
649+
int index = msg.lastIndexOf(Log.KEY_SYSTEM_INFO_DIVIDER);
650+
String info = index >= 0 ? msg.substring(index + Log.KEY_SYSTEM_INFO_DIVIDER.length()).trim()
651+
: "\n**环境信息** "
652+
+ "\n系统: " + System.getProperty("os.name") + " " + System.getProperty("os.version")
653+
+ "\nJDK: " + System.getProperty("java.version") + " " + System.getProperty("os.arch")
654+
+ "\n数据库: <!-- 请填写,例如 MySQL 5.7 -->"
655+
+ "\nAPIJSON: " + Log.VERSION;
656+
657+
msg = index < 0 ? msg : msg.substring(0, index).trim();
658+
String encodedMsg = URLEncoder.encode(msg, "UTF-8");
659+
660+
msg += " \n\n\n浏览器打开以下链接搜索答案\nGitHub: https://github.com/Tencent/APIJSON/issues?q=is%3Aissue+" + encodedMsg
661+
+ " \n\nGoogle:https://www.google.com/search?q=" + encodedMsg
662+
+ " \n\nBaidu:https://www.baidu.com/s?ie=UTF-8&wd=" + encodedMsg
663+
+ " \n\n都没找到答案?打开这个链接 https://github.com/Tencent/APIJSON/issues/new?assignees=&labels=&template=--bug.md "
664+
+ "\n然后提交问题,推荐用以下模板修改,注意要换行保持清晰可读。"
665+
+ "\n【标题】:" + msg
666+
+ "\n【内容】:" + info + "\n\n**问题描述**\n" + msg;
667+
} catch (Throwable e2) {
668+
e2.printStackTrace();
669+
}
670+
}
671+
644672
JSONObject error = newErrorResult(e);
645-
return extendResult(object, error.getIntValue(JSONResponse.KEY_CODE), error.getString(JSONResponse.KEY_MSG));
673+
return extendResult(object, error.getIntValue(JSONResponse.KEY_CODE), msg);
646674
}
647675
/**新建错误状态内容
648676
* @param e
@@ -1682,6 +1710,42 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
16821710
return result;
16831711
}
16841712
catch (Exception e) {
1713+
if (Log.DEBUG && e.getMessage().contains(Log.KEY_SYSTEM_INFO_DIVIDER) == false) {
1714+
try {
1715+
String db = config.getDatabase();
1716+
if (db == null) {
1717+
if (config.isPostgreSQL()) {
1718+
db = SQLConfig.DATABASE_POSTGRESQL;
1719+
}
1720+
else if (config.isSQLServer()) {
1721+
db = SQLConfig.DATABASE_SQLSERVER;
1722+
}
1723+
else if (config.isOracle()) {
1724+
db = SQLConfig.DATABASE_ORACLE;
1725+
}
1726+
else if (config.isDb2()) {
1727+
db = SQLConfig.DATABASE_DB2;
1728+
}
1729+
else if (config.isClickHouse()) {
1730+
db = SQLConfig.DATABASE_CLICKHOUSE;
1731+
}
1732+
else {
1733+
db = SQLConfig.DATABASE_MYSQL;
1734+
}
1735+
}
1736+
1737+
Class<? extends Exception> clazz = e.getClass();
1738+
e = clazz.getConstructor(String.class).newInstance(
1739+
e.getMessage()
1740+
+ " " + Log.KEY_SYSTEM_INFO_DIVIDER + " \n**环境信息** "
1741+
+ "\n系统: " + System.getProperty("os.name") + " " + System.getProperty("os.version")
1742+
+ "\nJDK: " + System.getProperty("java.version") + " " + System.getProperty("os.arch")
1743+
+ "\n数据库: " + db + " " + config.getDBVersion()
1744+
+ "\nAPIJSON: " + Log.VERSION
1745+
);
1746+
} catch (Throwable e2) {}
1747+
}
1748+
16851749
if (Log.DEBUG == false && e instanceof SQLException) {
16861750
throw new SQLException("数据库驱动执行异常SQLException,非 Log.DEBUG 模式下不显示详情,避免泄漏真实模式名、表名等隐私信息", e);
16871751
}

0 commit comments

Comments
 (0)