|
9 | 9 | import static apijson.RequestMethod.GET; |
10 | 10 |
|
11 | 11 | import java.io.UnsupportedEncodingException; |
| 12 | +import java.net.URLEncoder; |
12 | 13 | import java.sql.Connection; |
13 | 14 | import java.sql.SQLException; |
14 | 15 | import java.sql.Savepoint; |
@@ -641,8 +642,35 @@ public static JSONObject newSuccessResult() { |
641 | 642 | * @return |
642 | 643 | */ |
643 | 644 | 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 | + |
644 | 672 | 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); |
646 | 674 | } |
647 | 675 | /**新建错误状态内容 |
648 | 676 | * @param e |
@@ -1682,6 +1710,42 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except |
1682 | 1710 | return result; |
1683 | 1711 | } |
1684 | 1712 | 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 | + |
1685 | 1749 | if (Log.DEBUG == false && e instanceof SQLException) { |
1686 | 1750 | throw new SQLException("数据库驱动执行异常SQLException,非 Log.DEBUG 模式下不显示详情,避免泄漏真实模式名、表名等隐私信息", e); |
1687 | 1751 | } |
|
0 commit comments