From e7e2b07a9f88c5c3d407a49d69894281cee9de2c Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Fri, 29 Mar 2024 12:41:04 +0800 Subject: [PATCH] Refactor ServerSQLException (#30691) * Refactor ServerSQLException * Refactor ServerSQLException * Refactor ServerSQLException --- .../user-manual/error-code/sql-error-code.cn.md | 1 + .../user-manual/error-code/sql-error-code.en.md | 1 + .../sql/type/generic/ServerSQLException.java | 2 +- .../dialect/SQLExceptionTransformEngineTest.java | 15 ++++++++++++--- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/docs/document/content/user-manual/error-code/sql-error-code.cn.md b/docs/document/content/user-manual/error-code/sql-error-code.cn.md index b69e962821dc5..0df8e3049f00e 100644 --- a/docs/document/content/user-manual/error-code/sql-error-code.cn.md +++ b/docs/document/content/user-manual/error-code/sql-error-code.cn.md @@ -286,3 +286,4 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供, | 0A000 | 30001 | Unsupported SQL operation: %s | | 0A000 | 30002 | Database protocol exception: %s | | 0A000 | 30003 | Unsupported command: %s | +| 0A000 | 30004 | Server exception: %s | diff --git a/docs/document/content/user-manual/error-code/sql-error-code.en.md b/docs/document/content/user-manual/error-code/sql-error-code.en.md index 318ad0dc02ed4..8bb7e033d54c0 100644 --- a/docs/document/content/user-manual/error-code/sql-error-code.en.md +++ b/docs/document/content/user-manual/error-code/sql-error-code.en.md @@ -301,3 +301,4 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi | 0A000 | 30001 | Unsupported SQL operation: %s | | 0A000 | 30002 | Database protocol exception: %s | | 0A000 | 30003 | Unsupported command: %s | +| 0A000 | 30004 | Server exception: %s | diff --git a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/ServerSQLException.java b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/ServerSQLException.java index 3ffe3a108d302..2ba898650c27b 100644 --- a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/ServerSQLException.java +++ b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/generic/ServerSQLException.java @@ -27,6 +27,6 @@ public final class ServerSQLException extends GenericSQLException { private static final long serialVersionUID = -4072647406344887711L; public ServerSQLException(final Exception cause) { - super(cause.getMessage(), cause, XOpenSQLState.GENERAL_ERROR, 0); + super(String.format("Server exception: %s", cause.getMessage()), cause, XOpenSQLState.GENERAL_ERROR, 4); } } 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 841379c7f425e..86209afa77f7e 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 @@ -54,7 +54,10 @@ void assertToSQLExceptionWithShardingSphereSQLException() { void assertToSQLExceptionWithDatabaseProtocolException() { DatabaseProtocolException cause = mock(DatabaseProtocolException.class); when(cause.getMessage()).thenReturn("No reason"); - assertThat(SQLExceptionTransformEngine.toSQLException(cause, databaseType).getMessage(), is("Database protocol exception: No reason")); + SQLException actual = SQLExceptionTransformEngine.toSQLException(cause, databaseType); + assertThat(actual.getSQLState(), is("HY000")); + assertThat(actual.getErrorCode(), is(30002)); + assertThat(actual.getMessage(), is("Database protocol exception: No reason")); } @Test @@ -66,11 +69,17 @@ void assertToSQLExceptionWithSQLDialectException() { void assertToSQLExceptionWithShardingSphereServerException() { ShardingSphereServerException cause = mock(ShardingSphereServerException.class); when(cause.getMessage()).thenReturn("No reason"); - assertThat(SQLExceptionTransformEngine.toSQLException(cause, databaseType).getMessage(), is("No reason")); + SQLException actual = SQLExceptionTransformEngine.toSQLException(cause, databaseType); + assertThat(actual.getSQLState(), is("HY000")); + assertThat(actual.getErrorCode(), is(30004)); + assertThat(actual.getMessage(), is("Server exception: No reason")); } @Test void assertToSQLExceptionWithOtherException() { - assertThat(SQLExceptionTransformEngine.toSQLException(new Exception("No reason"), databaseType).getMessage(), is("Unknown exception: No reason")); + 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")); } }