This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 1669b783b46 Optimize the message of ShardingSphereSQLException.
(#32227)
1669b783b46 is described below
commit 1669b783b46b1702e1970beab2a470fb42ea8c90
Author: Cong Hu <[email protected]>
AuthorDate: Tue Jul 23 18:35:53 2024 +0800
Optimize the message of ShardingSphereSQLException. (#32227)
---
.../infra/exception/generic/UnknownSQLExceptionTest.java | 2 +-
.../core/external/sql/ShardingSphereSQLException.java | 15 ++++++---------
.../external/sql/type/kernel/KernelSQLExceptionTest.java | 2 +-
.../sql/type/wrapper/SQLWrapperExceptionTest.java | 2 +-
.../dialect/SQLExceptionTransformEngineTest.java | 4 ++--
.../connection/DriverDatabaseConnectionManagerTest.java | 5 +++--
.../frontend/mysql/err/MySQLErrorPacketFactoryTest.java | 2 +-
.../opengauss/err/OpenGaussErrorPacketFactoryTest.java | 2 +-
.../postgresql/err/PostgreSQLErrorPacketFactoryTest.java | 2 +-
9 files changed, 17 insertions(+), 19 deletions(-)
diff --git
a/infra/common/src/test/java/org/apache/shardingsphere/infra/exception/generic/UnknownSQLExceptionTest.java
b/infra/common/src/test/java/org/apache/shardingsphere/infra/exception/generic/UnknownSQLExceptionTest.java
index 9c9d91361ef..d44fec0df9e 100644
---
a/infra/common/src/test/java/org/apache/shardingsphere/infra/exception/generic/UnknownSQLExceptionTest.java
+++
b/infra/common/src/test/java/org/apache/shardingsphere/infra/exception/generic/UnknownSQLExceptionTest.java
@@ -32,6 +32,6 @@ class UnknownSQLExceptionTest {
SQLException actual = new UnknownSQLException(new
RuntimeException("foo_reason")).toSQLException();
assertThat(actual.getSQLState(),
is(XOpenSQLState.GENERAL_ERROR.getValue()));
assertThat(actual.getErrorCode(), is(30000));
- assertThat(actual.getMessage(), is("Unknown exception." +
System.lineSeparator() + "More details: foo_reason"));
+ assertThat(actual.getMessage(), is("Unknown exception." +
System.lineSeparator() + "More details: java.lang.RuntimeException:
foo_reason"));
}
}
diff --git
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
index 8e7d3c64a62..8b02a4a38ef 100644
---
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
+++
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.infra.exception.core.external.sql;
import com.google.common.base.Preconditions;
-import com.google.common.base.Strings;
import
org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException;
import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState;
@@ -38,10 +37,6 @@ public abstract class ShardingSphereSQLException extends
ShardingSphereExternalE
private final int vendorCode;
- private final String reason;
-
- private final Exception cause;
-
protected ShardingSphereSQLException(final SQLState sqlState, final int
typeOffset, final int errorCode, final String reason, final Object...
messageArgs) {
this(sqlState.getValue(), typeOffset, errorCode, formatMessage(reason,
messageArgs), null);
}
@@ -51,13 +46,15 @@ public abstract class ShardingSphereSQLException extends
ShardingSphereExternalE
}
protected ShardingSphereSQLException(final String sqlState, final int
typeOffset, final int errorCode, final String reason, final Exception cause) {
- super(reason, cause);
+ super(getMessage(reason, cause), cause);
this.sqlState = sqlState;
Preconditions.checkArgument(typeOffset >= 0 && typeOffset < 4, "The
value range of type offset should be [0, 3].");
Preconditions.checkArgument(errorCode >= 0 && errorCode < 10000, "The
value range of error code should be [0, 10000).");
vendorCode = typeOffset * 10000 + errorCode;
- this.reason = null == cause ||
Strings.isNullOrEmpty(cause.getMessage()) ? reason : String.format("%s%sMore
details: %s", reason, System.lineSeparator(), cause.getMessage());
- this.cause = cause;
+ }
+
+ private static String getMessage(final String reason, final Exception
cause) {
+ return null == cause ? reason : String.format("%s%sMore details: %s",
reason, System.lineSeparator(), cause);
}
private static String formatMessage(final String reason, final Object[]
messageArgs) {
@@ -81,6 +78,6 @@ public abstract class ShardingSphereSQLException extends
ShardingSphereExternalE
* @return SQL exception
*/
public final SQLException toSQLException() {
- return new SQLException(reason, sqlState, vendorCode, cause);
+ return new SQLException(getMessage(), sqlState, vendorCode,
getCause());
}
}
diff --git
a/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLExceptionTest.java
b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLExceptionTest.java
index 322eb3c8eeb..59533e0aaaa 100644
---
a/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLExceptionTest.java
+++
b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLExceptionTest.java
@@ -45,7 +45,7 @@ class KernelSQLExceptionTest {
}.toSQLException();
assertThat(actual.getSQLState(),
is(XOpenSQLState.GENERAL_ERROR.getValue()));
assertThat(actual.getErrorCode(), is(11001));
- assertThat(actual.getMessage(), is("reason" + System.lineSeparator() +
"More details: test"));
+ assertThat(actual.getMessage(), is("reason" + System.lineSeparator() +
"More details: java.lang.RuntimeException: test"));
assertThat(actual.getCause(), is(cause));
}
}
diff --git
a/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/wrapper/SQLWrapperExceptionTest.java
b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/wrapper/SQLWrapperExceptionTest.java
index c3ed9e32344..d3108a0a547 100644
---
a/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/wrapper/SQLWrapperExceptionTest.java
+++
b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/wrapper/SQLWrapperExceptionTest.java
@@ -31,6 +31,6 @@ class SQLWrapperExceptionTest {
SQLException actual = new SQLWrapperException(new
SQLException("reason", "1", 10)).toSQLException();
assertThat(actual.getSQLState(), is("1"));
assertThat(actual.getErrorCode(), is(10));
- assertThat(actual.getMessage(), is(System.lineSeparator() + "More
details: reason"));
+ assertThat(actual.getMessage(), is(System.lineSeparator() + "More
details: java.sql.SQLException: reason"));
}
}
diff --git
a/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/SQLExceptionTransformEngineTest.java
b/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/SQLExceptionTransformEngineTest.java
index c9ae3489d21..b2ccf3e9a10 100644
---
a/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/SQLExceptionTransformEngineTest.java
+++
b/infra/exception/dialect/core/src/test/java/org/apache/shardingsphere/infra/exception/dialect/SQLExceptionTransformEngineTest.java
@@ -72,7 +72,7 @@ class SQLExceptionTransformEngineTest {
SQLException actual =
SQLExceptionTransformEngine.toSQLException(cause, databaseType);
assertThat(actual.getSQLState(), is("HY000"));
assertThat(actual.getErrorCode(), is(30004));
- assertThat(actual.getMessage(), is("Server exception." +
System.lineSeparator() + "More details: No reason"));
+ assertThat(actual.getMessage(), is("Server exception." +
System.lineSeparator() + "More details: " + cause));
}
@Test
@@ -80,6 +80,6 @@ class SQLExceptionTransformEngineTest {
SQLException actual = SQLExceptionTransformEngine.toSQLException(new
Exception("No reason"), databaseType);
assertThat(actual.getSQLState(), is("HY000"));
assertThat(actual.getErrorCode(), is(30000));
- assertThat(actual.getMessage(), is("Unknown exception." +
System.lineSeparator() + "More details: No reason"));
+ assertThat(actual.getMessage(), is("Unknown exception." +
System.lineSeparator() + "More details: java.lang.Exception: No reason"));
}
}
diff --git
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
index 3c7af60607e..5f20a0fe983 100644
---
a/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
+++
b/jdbc/src/test/java/org/apache/shardingsphere/driver/jdbc/core/connection/DriverDatabaseConnectionManagerTest.java
@@ -76,7 +76,7 @@ class DriverDatabaseConnectionManagerTest {
Map<String, StorageUnit> result = new HashMap<>(2, 1F);
result.put("ds", mockStorageUnit(new MockedDataSource()));
DataSource invalidDataSource = mock(DataSource.class);
- when(invalidDataSource.getConnection()).thenThrow(new
SQLException(""));
+ when(invalidDataSource.getConnection()).thenThrow(new
SQLException("Mock invalid data source"));
result.put("invalid_ds", mockStorageUnit(invalidDataSource));
return result;
}
@@ -164,7 +164,8 @@ class DriverDatabaseConnectionManagerTest {
void assertGetConnectionsWhenConnectionCreateFailed() {
SQLException ex = assertThrows(SQLException.class, () ->
databaseConnectionManager.getConnections(DefaultDatabase.LOGIC_NAME,
"invalid_ds", 0, 3, ConnectionMode.CONNECTION_STRICTLY));
assertThat(ex.getMessage(), is("Can not get 3 connections one time,
partition succeed connection(0) have released. "
- + "Please consider increasing the 'maxPoolSize' of the data
sources or decreasing the 'max-connections-size-per-query' in properties."));
+ + "Please consider increasing the 'maxPoolSize' of the data
sources or decreasing the 'max-connections-size-per-query' in properties.\n"
+ + "More details: java.sql.SQLException: Mock invalid data
source"));
}
@Test
diff --git
a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrorPacketFactoryTest.java
b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrorPacketFactoryTest.java
index 9a5d0b45887..f9686569b38 100644
---
a/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrorPacketFactoryTest.java
+++
b/proxy/frontend/type/mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/err/MySQLErrorPacketFactoryTest.java
@@ -41,6 +41,6 @@ class MySQLErrorPacketFactoryTest {
MySQLErrPacket actual = MySQLErrorPacketFactory.newInstance(new
RuntimeException("No reason"));
assertThat(actual.getErrorCode(), is(30000));
assertThat(actual.getSqlState(),
is(XOpenSQLState.GENERAL_ERROR.getValue()));
- assertThat(actual.getErrorMessage(), is("Unknown exception." +
System.lineSeparator() + "More details: No reason"));
+ assertThat(actual.getErrorMessage(), is("Unknown exception." +
System.lineSeparator() + "More details: java.lang.RuntimeException: No
reason"));
}
}
diff --git
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/err/OpenGaussErrorPacketFactoryTest.java
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/err/OpenGaussErrorPacketFactoryTest.java
index 4e06074300e..bfc4dba0ea5 100644
---
a/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/err/OpenGaussErrorPacketFactoryTest.java
+++
b/proxy/frontend/type/opengauss/src/test/java/org/apache/shardingsphere/proxy/frontend/opengauss/err/OpenGaussErrorPacketFactoryTest.java
@@ -74,7 +74,7 @@ class OpenGaussErrorPacketFactoryTest {
assertThat(actualFields.size(), is(4));
assertThat(actualFields.get(OpenGaussErrorResponsePacket.FIELD_TYPE_SEVERITY),
is("ERROR"));
assertThat(actualFields.get(OpenGaussErrorResponsePacket.FIELD_TYPE_CODE),
is("58000"));
-
assertThat(actualFields.get(OpenGaussErrorResponsePacket.FIELD_TYPE_MESSAGE),
is("Unknown exception." + System.lineSeparator() + "More details: No reason"));
+
assertThat(actualFields.get(OpenGaussErrorResponsePacket.FIELD_TYPE_MESSAGE),
is("Unknown exception." + System.lineSeparator() + "More details:
java.lang.RuntimeException: No reason"));
assertThat(actualFields.get(OpenGaussErrorResponsePacket.FIELD_TYPE_ERROR_CODE),
is("0"));
}
diff --git
a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactoryTest.java
b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactoryTest.java
index 61530804985..628fd9fe7f1 100644
---
a/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactoryTest.java
+++
b/proxy/frontend/type/postgresql/src/test/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactoryTest.java
@@ -62,6 +62,6 @@ class PostgreSQLErrorPacketFactoryTest {
void assertRuntimeException() throws ReflectiveOperationException {
PostgreSQLErrorResponsePacket actual =
PostgreSQLErrorPacketFactory.newInstance(new RuntimeException("No reason"));
Map<Character, String> fields = (Map<Character, String>)
Plugins.getMemberAccessor().get(PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"),
actual);
-
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE),
is("Unknown exception." + System.lineSeparator() + "More details: No reason"));
+
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE),
is("Unknown exception." + System.lineSeparator() + "More details:
java.lang.RuntimeException: No reason"));
}
}