Skip to content

Commit

Permalink
Refactor ServerSQLException (#30691)
Browse files Browse the repository at this point in the history
* Refactor ServerSQLException

* Refactor ServerSQLException

* Refactor ServerSQLException
  • Loading branch information
terrymanu authored Mar 29, 2024
1 parent 5f704c4 commit e7e2b07
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"));
}
}

0 comments on commit e7e2b07

Please sign in to comment.