Skip to content

Commit

Permalink
Refactor ShardingSphereSQLException (#30761)
Browse files Browse the repository at this point in the history
* Refactor ShardingSphereSQLException

* Refactor ShardingSphereSQLException

* Refactor ShardingSphereSQLException

* Refactor ShardingSphereSQLException

* Refactor ShardingSphereSQLException

* Refactor ShardingSphereSQLException

* Refactor ShardingSphereSQLException

* Refactor ShardingSphereSQLException
  • Loading branch information
terrymanu authored Apr 3, 2024
1 parent 15b2a80 commit 721c0ca
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ 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
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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@ void assertPSQLExceptionWithServerErrorMessageIsNull() throws ReflectiveOperatio
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: No reason"));
assertThat(fields.get(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE), is("Unknown exception." + System.lineSeparator() + "More details: No reason"));
}
}

0 comments on commit 721c0ca

Please sign in to comment.