From 0ab150a1e435ce58792311ff0a0d3a878d6b3817 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 18 Apr 2024 04:42:16 +0800 Subject: [PATCH] Refactor ShardingSphereSQLException (#30944) --- .../OverallConnectionNotEnoughException.java | 4 ++-- .../resource/ResourceDefinitionException.java | 4 ++-- .../StorageUnitsOperateException.java | 2 +- .../external/sql/ShardingSphereSQLException.java | 16 +++------------- .../sql/type/kernel/KernelSQLException.java | 4 ++-- .../kernel/category/ClusterSQLException.java | 4 ++-- .../kernel/category/ConnectionSQLException.java | 4 ++-- .../kernel/category/MetaDataSQLException.java | 4 ++-- .../kernel/category/TransactionSQLException.java | 4 ++-- .../sql/type/kernel/KernelSQLExceptionTest.java | 2 +- .../exception/DriverConnectionException.java | 4 ++-- .../exception/DriverRegisterException.java | 2 +- .../CloseTransactionManagerFailedException.java | 2 +- .../XATransactionPrivilegeCheckException.java | 2 +- .../jta/exception/XATransactionSQLException.java | 4 ++-- .../ClusterPersistRepositoryException.java | 2 +- 16 files changed, 27 insertions(+), 37 deletions(-) diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/connection/OverallConnectionNotEnoughException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/connection/OverallConnectionNotEnoughException.java index 8674a2f7e6eac..2be4cf3803158 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/connection/OverallConnectionNotEnoughException.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/connection/OverallConnectionNotEnoughException.java @@ -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); } } diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/ResourceDefinitionException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/ResourceDefinitionException.java index b02e855a03afc..240671674c1fb 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/ResourceDefinitionException.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/ResourceDefinitionException.java @@ -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) { diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsOperateException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsOperateException.java index 71cedf2faf438..9a3b2627d610f 100644 --- a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsOperateException.java +++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/resource/storageunit/StorageUnitsOperateException.java @@ -30,6 +30,6 @@ public final class StorageUnitsOperateException extends ResourceDefinitionExcept private static final long serialVersionUID = 7029641448948791509L; public StorageUnitsOperateException(final String operationType, final Collection 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); } } 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 4e790a63add89..a2e7ac9de6f74 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 @@ -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) { @@ -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; } diff --git a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLException.java b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLException.java index c4db8e52b51b5..874b87388ab41 100644 --- a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLException.java +++ b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLException.java @@ -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) { diff --git a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/ClusterSQLException.java b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/ClusterSQLException.java index 07da6d1a8ba95..b4bf031d3e94d 100644 --- a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/ClusterSQLException.java +++ b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/ClusterSQLException.java @@ -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); } } diff --git a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/ConnectionSQLException.java b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/ConnectionSQLException.java index dd08c471986cc..cfb1c5648e4f0 100644 --- a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/ConnectionSQLException.java +++ b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/ConnectionSQLException.java @@ -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); } } diff --git a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/MetaDataSQLException.java b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/MetaDataSQLException.java index 5e19f43fb2896..746f86f151869 100644 --- a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/MetaDataSQLException.java +++ b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/MetaDataSQLException.java @@ -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); } } diff --git a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/TransactionSQLException.java b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/TransactionSQLException.java index 56ddf109b8163..d97b0392e647f 100644 --- a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/TransactionSQLException.java +++ b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/category/TransactionSQLException.java @@ -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); } } 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 4dd577e91f290..322eb3c8eebc3 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 @@ -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)); diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/exception/DriverConnectionException.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/exception/DriverConnectionException.java index c09d81e8762fa..658c7b5c28ce5 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/exception/DriverConnectionException.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/exception/DriverConnectionException.java @@ -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) { diff --git a/jdbc/src/main/java/org/apache/shardingsphere/driver/exception/DriverRegisterException.java b/jdbc/src/main/java/org/apache/shardingsphere/driver/exception/DriverRegisterException.java index 1a08a4284f1be..eb3fa8cf63c73 100644 --- a/jdbc/src/main/java/org/apache/shardingsphere/driver/exception/DriverRegisterException.java +++ b/jdbc/src/main/java/org/apache/shardingsphere/driver/exception/DriverRegisterException.java @@ -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."); } } diff --git a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/CloseTransactionManagerFailedException.java b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/CloseTransactionManagerFailedException.java index c7e4bf05f0b00..528f8670fb984 100644 --- a/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/CloseTransactionManagerFailedException.java +++ b/kernel/transaction/core/src/main/java/org/apache/shardingsphere/transaction/exception/CloseTransactionManagerFailedException.java @@ -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."); } } diff --git a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionPrivilegeCheckException.java b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionPrivilegeCheckException.java index 1748fd9bc390e..45c971c5b5865 100644 --- a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionPrivilegeCheckException.java +++ b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionPrivilegeCheckException.java @@ -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); } } diff --git a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionSQLException.java b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionSQLException.java index ee2c4d9653cbb..7a4d445356479 100644 --- a/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionSQLException.java +++ b/kernel/transaction/type/xa/core/src/main/java/org/apache/shardingsphere/transaction/xa/jta/exception/XATransactionSQLException.java @@ -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) { diff --git a/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/exception/ClusterPersistRepositoryException.java b/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/exception/ClusterPersistRepositoryException.java index eed877f86fca8..c2d7ee1e3a9fe 100644 --- a/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/exception/ClusterPersistRepositoryException.java +++ b/mode/type/cluster/repository/api/src/main/java/org/apache/shardingsphere/mode/repository/cluster/exception/ClusterPersistRepositoryException.java @@ -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."); } }