Skip to content

Commit

Permalink
Refactor ShardingSphereSQLException (#30944)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Apr 17, 2024
1 parent 3687698 commit 0ab150a
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class OverallConnectionNotEnoughException extends ConnectionSQLExce
private static final long serialVersionUID = -1297088138042287804L;

public OverallConnectionNotEnoughException(final int desiredSize, final int actualSize, final Exception cause) {
super(XOpenSQLState.CONNECTION_EXCEPTION, 0, String.format("Can not get %d connections one time, partition succeed connection(%d) have released. "
+ "Please consider increasing the 'maxPoolSize' of the data sources or decreasing the 'max-connections-size-per-query' in properties.", desiredSize, actualSize), cause);
super(XOpenSQLState.CONNECTION_EXCEPTION, 0, cause, "Can not get %d connections one time, partition succeed connection(%d) have released. "
+ "Please consider increasing the 'maxPoolSize' of the data sources or decreasing the 'max-connections-size-per-query' in properties.", desiredSize, actualSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected ResourceDefinitionException(final SQLState sqlState, final int errorCo
super(sqlState, getErrorCode(errorCode), reason, messageArgs);
}

protected ResourceDefinitionException(final SQLState sqlState, final int errorCode, final String reason, final Exception cause) {
super(sqlState, getErrorCode(errorCode), reason, cause);
protected ResourceDefinitionException(final SQLState sqlState, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
super(sqlState, getErrorCode(errorCode), cause, reason, messageArgs);
}

private static int getErrorCode(final int errorCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public final class StorageUnitsOperateException extends ResourceDefinitionExcept
private static final long serialVersionUID = 7029641448948791509L;

public StorageUnitsOperateException(final String operationType, final Collection<String> storageUnitNames, final Exception cause) {
super(XOpenSQLState.GENERAL_ERROR, 0, String.format("Can not %s storage units '%s'.", operationType, storageUnitNames), cause);
super(XOpenSQLState.GENERAL_ERROR, 0, cause, "Can not %s storage units '%s'.", operationType, storageUnitNames);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,11 @@ public abstract class ShardingSphereSQLException extends ShardingSphereExternalE
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, reason, messageArgs);
this(sqlState.getValue(), typeOffset, errorCode, null == reason ? null : String.format(reason, formatMessageArguments(messageArgs)), null);
}

protected ShardingSphereSQLException(final SQLState sqlState, final int typeOffset, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
this(sqlState.getValue(), typeOffset, errorCode, cause, reason, messageArgs);
}

protected ShardingSphereSQLException(final String sqlState, final int typeOffset, final int errorCode, final String reason, final Object... messageArgs) {
this(sqlState, typeOffset, errorCode, null == reason ? null : String.format(reason, formatMessageArguments(messageArgs)), (Exception) null);
}

protected ShardingSphereSQLException(final String sqlState, final int typeOffset, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
this(sqlState, typeOffset, errorCode, null == reason ? null : String.format(reason, formatMessageArguments(messageArgs)), cause);
this(sqlState.getValue(), typeOffset, errorCode, null == reason ? null : String.format(reason, formatMessageArguments(messageArgs)), cause);
}

protected ShardingSphereSQLException(final String sqlState, final int typeOffset, final int errorCode, final String reason, final Exception cause) {
Expand All @@ -64,9 +56,7 @@ 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 = null == cause || Strings.isNullOrEmpty(cause.getMessage())
? reason
: String.format("%s%sMore details: %s", reason, System.lineSeparator(), cause.getMessage());
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 @@ -34,8 +34,8 @@ protected KernelSQLException(final SQLState sqlState, final int kernelCode, fina
super(sqlState, TYPE_OFFSET, getErrorCode(kernelCode, errorCode), reason, messageArgs);
}

protected KernelSQLException(final SQLState sqlState, final int kernelCode, final int errorCode, final String reason, final Exception cause) {
super(sqlState.getValue(), TYPE_OFFSET, getErrorCode(kernelCode, errorCode), reason, cause);
protected KernelSQLException(final SQLState sqlState, final int kernelCode, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
super(sqlState, TYPE_OFFSET, getErrorCode(kernelCode, errorCode), cause, reason, messageArgs);
}

private static int getErrorCode(final int kernelCode, final int errorCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected ClusterSQLException(final SQLState sqlState, final int errorCode, fina
super(sqlState, KERNEL_CODE, errorCode, reason, messageArgs);
}

protected ClusterSQLException(final SQLState sqlState, final int errorCode, final String reason, final Exception cause) {
super(sqlState, KERNEL_CODE, errorCode, reason, cause);
protected ClusterSQLException(final SQLState sqlState, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
super(sqlState, KERNEL_CODE, errorCode, cause, reason, messageArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected ConnectionSQLException(final SQLState sqlState, final int errorCode, f
super(sqlState, KERNEL_CODE, errorCode, reason, messageArgs);
}

protected ConnectionSQLException(final SQLState sqlState, final int errorCode, final String reason, final Exception cause) {
super(sqlState, KERNEL_CODE, errorCode, reason, cause);
protected ConnectionSQLException(final SQLState sqlState, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
super(sqlState, KERNEL_CODE, errorCode, cause, reason, messageArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected MetaDataSQLException(final SQLState sqlState, final int errorCode, fin
super(sqlState, KERNEL_CODE, errorCode, reason, messageArgs);
}

protected MetaDataSQLException(final SQLState sqlState, final int errorCode, final String reason, final Exception cause) {
super(sqlState, KERNEL_CODE, errorCode, reason, cause);
protected MetaDataSQLException(final SQLState sqlState, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
super(sqlState, KERNEL_CODE, errorCode, cause, reason, messageArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected TransactionSQLException(final SQLState sqlState, final int errorCode,
super(sqlState, KERNEL_CODE, errorCode, reason, messageArgs);
}

protected TransactionSQLException(final SQLState sqlState, final int errorCode, final String reason, final Exception cause) {
super(sqlState, KERNEL_CODE, errorCode, reason, cause);
protected TransactionSQLException(final SQLState sqlState, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
super(sqlState, KERNEL_CODE, errorCode, cause, reason, messageArgs);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void assertToSQLException() {
@Test
void assertToSQLExceptionWithCause() {
Exception cause = new RuntimeException("test");
SQLException actual = new KernelSQLException(XOpenSQLState.GENERAL_ERROR, 1, 1, "reason", cause) {
SQLException actual = new KernelSQLException(XOpenSQLState.GENERAL_ERROR, 1, 1, cause, "reason") {
}.toSQLException();
assertThat(actual.getSQLState(), is(XOpenSQLState.GENERAL_ERROR.getValue()));
assertThat(actual.getErrorCode(), is(11001));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected DriverConnectionException(final SQLState sqlState, final int errorCode
super(sqlState, getErrorCode(errorCode), reason, messageArgs);
}

protected DriverConnectionException(final SQLState sqlState, final int errorCode, final String reason, final Exception cause) {
super(sqlState, getErrorCode(errorCode), reason, cause);
protected DriverConnectionException(final SQLState sqlState, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
super(sqlState, getErrorCode(errorCode), cause, reason, messageArgs);
}

private static int getErrorCode(final int errorCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public final class DriverRegisterException extends DriverConnectionException {
private static final long serialVersionUID = -8091239932993280564L;

public DriverRegisterException(final SQLException cause) {
super(XOpenSQLState.CONNECTION_EXCEPTION, 0, "Can not register driver.", cause);
super(XOpenSQLState.CONNECTION_EXCEPTION, 0, cause, "Can not register driver.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public final class CloseTransactionManagerFailedException extends TransactionSQL
private static final long serialVersionUID = -3396778990357223580L;

public CloseTransactionManagerFailedException(final Exception cause) {
super(XOpenSQLState.INVALID_TRANSACTION_STATE, 4, "Close transaction manager failed.", cause);
super(XOpenSQLState.INVALID_TRANSACTION_STATE, 4, cause, "Close transaction manager failed.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ public XATransactionPrivilegeCheckException(final String privilege) {
}

public XATransactionPrivilegeCheckException(final String privilege, final SQLException cause) {
super(XOpenSQLState.INVALID_TRANSACTION_STATE, 2, String.format("Check XA transaction privileges failed on data source, please grant '%s' to current user.", privilege), cause);
super(XOpenSQLState.INVALID_TRANSACTION_STATE, 2, cause, "Check XA transaction privileges failed on data source, please grant '%s' to current user.", privilege);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ protected XATransactionSQLException(final SQLState sqlState, final int errorCode
super(sqlState, getErrorCode(errorCode), reason, messageArgs);
}

protected XATransactionSQLException(final SQLState sqlState, final int errorCode, final String reason, final Exception cause) {
super(sqlState, getErrorCode(errorCode), reason, cause);
protected XATransactionSQLException(final SQLState sqlState, final int errorCode, final Exception cause, final String reason, final Object... messageArgs) {
super(sqlState, getErrorCode(errorCode), cause, reason, messageArgs);
}

private static int getErrorCode(final int errorCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public final class ClusterPersistRepositoryException extends ClusterSQLException
private static final long serialVersionUID = -6417179023552012152L;

public ClusterPersistRepositoryException(final Exception cause) {
super(XOpenSQLState.GENERAL_ERROR, 10, "Cluster persist repository error.", cause);
super(XOpenSQLState.GENERAL_ERROR, 10, cause, "Cluster persist repository error.");
}
}

0 comments on commit 0ab150a

Please sign in to comment.