From 721c0caeb7000d5648ef2b8e1fd7a8a75f400b17 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Wed, 3 Apr 2024 23:41:32 +0800 Subject: [PATCH] Refactor ShardingSphereSQLException (#30761) * Refactor ShardingSphereSQLException * Refactor ShardingSphereSQLException * Refactor ShardingSphereSQLException * Refactor ShardingSphereSQLException * Refactor ShardingSphereSQLException * Refactor ShardingSphereSQLException * Refactor ShardingSphereSQLException * Refactor ShardingSphereSQLException --- .../core/external/sql/ShardingSphereSQLException.java | 5 ++++- .../core/external/sql/type/generic/UnknownSQLException.java | 2 +- .../core/external/sql/type/wrapper/SQLWrapperException.java | 2 +- .../external/sql/type/generic/UnknownSQLExceptionTest.java | 2 +- .../external/sql/type/kernel/KernelSQLExceptionTest.java | 2 +- .../external/sql/type/wrapper/SQLWrapperExceptionTest.java | 2 +- .../exception/dialect/SQLExceptionTransformEngineTest.java | 4 ++-- .../frontend/mysql/err/MySQLErrorPacketFactoryTest.java | 2 +- .../opengauss/err/OpenGaussErrorPacketFactoryTest.java | 2 +- .../postgresql/err/PostgreSQLErrorPacketFactoryTest.java | 2 +- 10 files changed, 14 insertions(+), 11 deletions(-) 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 758c7323f50b3..51a47445c56b1 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,6 +18,7 @@ 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; @@ -52,7 +53,9 @@ protected ShardingSphereSQLException(final String sqlState, final int typeOffset 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 = reason; + this.reason = null == cause || Strings.isNullOrEmpty(cause.getMessage()) + ? reason + : String.format("%s%sMore details: %s", reason, System.lineSeparator(), cause.getMessage()); this.cause = cause; } diff --git a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/UnknownSQLException.java b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/UnknownSQLException.java index 5227219e74266..f538f64a91a80 100644 --- a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/UnknownSQLException.java +++ b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/UnknownSQLException.java @@ -27,6 +27,6 @@ public final class UnknownSQLException extends GenericSQLException { private static final long serialVersionUID = -7357918573504734977L; public UnknownSQLException(final Exception cause) { - super(String.format("Unknown exception: %s", cause.getMessage()), cause, XOpenSQLState.GENERAL_ERROR, 0); + super("Unknown exception.", cause, XOpenSQLState.GENERAL_ERROR, 0); } } diff --git a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/wrapper/SQLWrapperException.java b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/wrapper/SQLWrapperException.java index 5d3acd3dcc783..5b1ef7f6e5f43 100644 --- a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/wrapper/SQLWrapperException.java +++ b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/wrapper/SQLWrapperException.java @@ -31,6 +31,6 @@ public final class SQLWrapperException extends ShardingSphereSQLException { private static final int TYPE_OFFSET = 0; public SQLWrapperException(final SQLException cause) { - super(cause.getSQLState(), TYPE_OFFSET, cause.getErrorCode(), cause.getMessage(), cause); + super(cause.getSQLState(), TYPE_OFFSET, cause.getErrorCode(), "", cause); } } diff --git a/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/UnknownSQLExceptionTest.java b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/UnknownSQLExceptionTest.java index ff301bc07120e..538ec9b868df6 100644 --- a/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/UnknownSQLExceptionTest.java +++ b/infra/exception/core/src/test/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/UnknownSQLExceptionTest.java @@ -32,6 +32,6 @@ void assertToSQLException() { 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: foo_reason")); + assertThat(actual.getMessage(), is("Unknown exception." + System.lineSeparator() + "More details: foo_reason")); } } 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 0f2d36eb62193..4dd577e91f290 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 @@ void assertToSQLExceptionWithCause() { }.toSQLException(); assertThat(actual.getSQLState(), is(XOpenSQLState.GENERAL_ERROR.getValue())); assertThat(actual.getErrorCode(), is(11001)); - assertThat(actual.getMessage(), is("reason")); + assertThat(actual.getMessage(), is("reason" + System.lineSeparator() + "More details: 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 58a15ec6e33ee..c3ed9e3234431 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 @@ void assertToSQLException() { SQLException actual = new SQLWrapperException(new SQLException("reason", "1", 10)).toSQLException(); assertThat(actual.getSQLState(), is("1")); assertThat(actual.getErrorCode(), is(10)); - assertThat(actual.getMessage(), is("reason")); + assertThat(actual.getMessage(), is(System.lineSeparator() + "More details: 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 86209afa77f7e..bd2df85ef15d0 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 @@ void assertToSQLExceptionWithShardingSphereServerException() { SQLException actual = SQLExceptionTransformEngine.toSQLException(cause, databaseType); assertThat(actual.getSQLState(), is("HY000")); assertThat(actual.getErrorCode(), is(30004)); - assertThat(actual.getMessage(), is("Server exception: No reason")); + assertThat(actual.getMessage(), is("Server exception: No reason" + System.lineSeparator() + "More details: No reason")); } @Test @@ -80,6 +80,6 @@ void assertToSQLExceptionWithOtherException() { 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: No reason")); + assertThat(actual.getMessage(), is("Unknown exception." + System.lineSeparator() + "More details: No reason")); } } 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 847522da441a2..9a5d0b4588750 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 @@ void assertNewInstanceWithSQLState() { 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: No reason")); + assertThat(actual.getErrorMessage(), is("Unknown exception." + System.lineSeparator() + "More details: 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 689be82bc5e44..5903aa02a7752 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 @@ void assertNewInstanceWithUnknownException() { 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: No reason")); + assertThat(actualFields.get(OpenGaussErrorResponsePacket.FIELD_TYPE_MESSAGE), is("Unknown exception." + System.lineSeparator() + "More details: No reason")); assertThat(actualFields.get(OpenGaussErrorResponsePacket.FIELD_TYPE_ERRORCODE), 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 b6cc9aba9a1cb..6153080498526 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 @@ void assertPSQLExceptionWithServerErrorMessageIsNull() throws ReflectiveOperatio void assertRuntimeException() throws ReflectiveOperationException { PostgreSQLErrorResponsePacket actual = PostgreSQLErrorPacketFactory.newInstance(new RuntimeException("No reason")); Map fields = (Map) Plugins.getMemberAccessor().get(PostgreSQLErrorResponsePacket.class.getDeclaredField("fields"), actual); - assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), is("Unknown exception: No reason")); + assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), is("Unknown exception." + System.lineSeparator() + "More details: No reason")); } }