From 6bb6e162247060c269c5740fe3fbfa5ff2a2097c Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Mon, 25 Mar 2024 23:39:04 +0800 Subject: [PATCH] Refactor UnregisteredAlgorithmException (#30646) * Refactor UnregisteredAlgorithmException * Refactor UnregisteredAlgorithmException --- .../error-code/sql-error-code.cn.md | 3 +- .../error-code/sql-error-code.en.md | 3 +- .../EncryptRuleConfigurationChecker.java | 10 +++--- .../UnregisteredEncryptorException.java | 33 ------------------- .../EncryptRuleConfigurationCheckerTest.java | 8 ++--- .../checker/MaskRuleConfigurationChecker.java | 4 +-- ...va => UnregisteredAlgorithmException.java} | 8 ++--- 7 files changed, 17 insertions(+), 52 deletions(-) delete mode 100644 features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/metadata/UnregisteredEncryptorException.java rename infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/{AlgorithmNotFoundOnColumnException.java => UnregisteredAlgorithmException.java} (72%) 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 300e85e1b4ad8..0d43dee8368b7 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 @@ -24,7 +24,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供, | 42000 | 10010 | Rule does not exist. | | 44000 | 10011 | Algorithm '%s.'%s' initialization failed, reason is: %s. | | 44000 | 10012 | '%s' algorithm on database.table.column: '%s'.'%s'.'%s' is required. | -| 42S02 | 10013 | Can not find '%s' algorithm '%s' on database.table.column: '%s'.'%s'.'%s'. | +| 42S02 | 10013 | '%s' algorithm '%s' on database.table.column: '%s'.'%s'.'%s' is unregistered. | | HY000 | 10015 | Algorithm '%s.%s' execute failed, reason is: %s. | | 42S02 | 10020 | Schema \`%s\` does not exist. | | 42S02 | 10021 | Single table \`%s\` does not exist. | @@ -269,7 +269,6 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供, | 44000 | 20705 | Assisted query column of \`%s\` can not be null in database \`%s\`. | | 44000 | 20707 | Like query column of \`%s\` can not be null in database \`%s\`. | | 44000 | 20709 | Can not find encrypt table: \`%s\`. | -| 44000 | 20710 | Can not found registered encryptor \`%s\` in database \`%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 5370722c3e086..7f5fcd4850ad7 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 @@ -24,7 +24,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi | 42000 | 10010 | Rule does not exist. | | 44000 | 10011 | Algorithm '%s.'%s' initialization failed, reason is: %s. | | 44000 | 10012 | '%s' algorithm on database.table.column: '%s'.'%s'.'%s' is required. | -| 42S02 | 10013 | Can not find '%s' algorithm '%s' on database.table.column: '%s'.'%s'.'%s'. | +| 42S02 | 10013 | '%s' algorithm '%s' on database.table.column: '%s'.'%s'.'%s' is unregistered. | | HY000 | 10015 | Algorithm '%s.%s' execute failed, reason is: %s. | | 42S02 | 10020 | Schema \`%s\` does not exist. | | 42S02 | 10021 | Single table \`%s\` does not exist. | @@ -284,7 +284,6 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi | 44000 | 20705 | Assisted query column of \`%s\` can not be null in database \`%s\`. | | 44000 | 20707 | Like query column of \`%s\` can not be null in database \`%s\`. | | 44000 | 20709 | Can not find encrypt table: \`%s\`. | -| 44000 | 20710 | Can not found registered encryptor \`%s\` in database \`%s\`. | ### Shadow Database diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationChecker.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationChecker.java index 32cbd77d4f137..0912cea66c725 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationChecker.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationChecker.java @@ -26,10 +26,10 @@ import org.apache.shardingsphere.encrypt.exception.metadata.EncryptAssistedQueryColumnNotFoundException; import org.apache.shardingsphere.encrypt.exception.metadata.EncryptCipherColumnNotFoundException; import org.apache.shardingsphere.encrypt.exception.metadata.EncryptLikeQueryColumnNotFoundException; -import org.apache.shardingsphere.encrypt.exception.metadata.UnregisteredEncryptorException; import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; import org.apache.shardingsphere.infra.algorithm.core.exception.EmptyAlgorithmOnColumnException; +import org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException; import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; @@ -75,7 +75,7 @@ private void checkCipherColumn(final String databaseName, final String tableName ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(cipherColumnConfig.getEncryptorName()), () -> new EmptyAlgorithmOnColumnException("Standard encrypt", databaseName, tableName, logicColumnName)); ShardingSpherePreconditions.checkState(encryptors.containsKey(cipherColumnConfig.getEncryptorName()), - () -> new UnregisteredEncryptorException(databaseName, cipherColumnConfig.getEncryptorName())); + () -> new UnregisteredAlgorithmException("Standard encrypt", cipherColumnConfig.getEncryptorName(), databaseName, tableName, logicColumnName)); } private void checkAssistColumn(final String databaseName, final String tableName, final String logicColumnName, @@ -83,8 +83,8 @@ private void checkAssistColumn(final String databaseName, final String tableName ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(assistedQueryColumnConfig.getName()), () -> new EncryptAssistedQueryColumnNotFoundException(logicColumnName, databaseName)); ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(assistedQueryColumnConfig.getEncryptorName()), () -> new EmptyAlgorithmOnColumnException("Assist query encrypt", databaseName, tableName, logicColumnName)); - ShardingSpherePreconditions.checkState( - encryptors.containsKey(assistedQueryColumnConfig.getEncryptorName()), () -> new UnregisteredEncryptorException(databaseName, assistedQueryColumnConfig.getEncryptorName())); + ShardingSpherePreconditions.checkState(encryptors.containsKey(assistedQueryColumnConfig.getEncryptorName()), + () -> new UnregisteredAlgorithmException("Assist query encrypt", assistedQueryColumnConfig.getEncryptorName(), databaseName, tableName, logicColumnName)); } private void checkLikeColumn(final String databaseName, final String tableName, final String logicColumnName, @@ -93,7 +93,7 @@ private void checkLikeColumn(final String databaseName, final String tableName, ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(likeQueryColumnConfig.getEncryptorName()), () -> new EmptyAlgorithmOnColumnException("Like query", databaseName, tableName, logicColumnName)); ShardingSpherePreconditions.checkState(encryptors.containsKey(likeQueryColumnConfig.getEncryptorName()), - () -> new UnregisteredEncryptorException(databaseName, likeQueryColumnConfig.getEncryptorName())); + () -> new UnregisteredAlgorithmException("Like query encrypt", likeQueryColumnConfig.getEncryptorName(), databaseName, tableName, logicColumnName)); } @Override diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/metadata/UnregisteredEncryptorException.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/metadata/UnregisteredEncryptorException.java deleted file mode 100644 index 680c097dc3d95..0000000000000 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/exception/metadata/UnregisteredEncryptorException.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.shardingsphere.encrypt.exception.metadata; - -import org.apache.shardingsphere.encrypt.exception.EncryptSQLException; -import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.XOpenSQLState; - -/** - * Unregistered encryptor exception. - */ -public final class UnregisteredEncryptorException extends EncryptSQLException { - - private static final long serialVersionUID = -1106379155485560944L; - - public UnregisteredEncryptorException(final String databaseName, final String encryptorName) { - super(XOpenSQLState.CHECK_OPTION_VIOLATION, 10, "Can not found registered encryptor `%s` in database `%s`.", encryptorName, databaseName); - } -} diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationCheckerTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationCheckerTest.java index 0075f3ae01c0f..a8d2e09a9bb82 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationCheckerTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/EncryptRuleConfigurationCheckerTest.java @@ -21,8 +21,8 @@ import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnItemRuleConfiguration; import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration; import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration; -import org.apache.shardingsphere.encrypt.exception.metadata.UnregisteredEncryptorException; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; +import org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException; import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker; import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader; import org.junit.jupiter.api.Test; @@ -59,7 +59,7 @@ private EncryptRuleConfiguration createValidConfiguration() { void assertCheckWhenConfigInvalidCipherColumn() { EncryptRuleConfiguration config = createInvalidCipherColumnConfig(); RuleConfigurationChecker checker = OrderedSPILoader.getServicesByClass(RuleConfigurationChecker.class, Collections.singleton(config.getClass())).get(config.getClass()); - assertThrows(UnregisteredEncryptorException.class, () -> checker.check("test", config, Collections.emptyMap(), Collections.emptyList())); + assertThrows(UnregisteredAlgorithmException.class, () -> checker.check("test", config, Collections.emptyMap(), Collections.emptyList())); } private EncryptRuleConfiguration createInvalidCipherColumnConfig() { @@ -76,7 +76,7 @@ private EncryptRuleConfiguration createInvalidCipherColumnConfig() { void assertCheckWhenConfigInvalidAssistColumn() { EncryptRuleConfiguration config = createInvalidAssistColumnConfig(); RuleConfigurationChecker checker = OrderedSPILoader.getServicesByClass(RuleConfigurationChecker.class, Collections.singleton(config.getClass())).get(config.getClass()); - assertThrows(UnregisteredEncryptorException.class, () -> checker.check("test", config, Collections.emptyMap(), Collections.emptyList())); + assertThrows(UnregisteredAlgorithmException.class, () -> checker.check("test", config, Collections.emptyMap(), Collections.emptyList())); } private EncryptRuleConfiguration createInvalidAssistColumnConfig() { @@ -94,7 +94,7 @@ private EncryptRuleConfiguration createInvalidAssistColumnConfig() { void assertCheckWhenConfigInvalidLikeColumn() { EncryptRuleConfiguration config = createInvalidLikeColumnConfig(); RuleConfigurationChecker checker = OrderedSPILoader.getServicesByClass(RuleConfigurationChecker.class, Collections.singleton(config.getClass())).get(config.getClass()); - assertThrows(UnregisteredEncryptorException.class, () -> checker.check("test", config, Collections.emptyMap(), Collections.emptyList())); + assertThrows(UnregisteredAlgorithmException.class, () -> checker.check("test", config, Collections.emptyMap(), Collections.emptyList())); } private EncryptRuleConfiguration createInvalidLikeColumnConfig() { diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java index 4a5d7c7047115..d7cfda42b7e9b 100644 --- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java +++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/checker/MaskRuleConfigurationChecker.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.mask.checker; import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration; -import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmNotFoundOnColumnException; +import org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException; import org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; @@ -59,7 +59,7 @@ private void checkColumns(final String databaseName, final MaskTableRuleConfigur private void checkColumn(final String databaseName, final String tableName, final MaskColumnRuleConfiguration columnRuleConfig, final Map maskAlgorithms) { ShardingSpherePreconditions.checkState(maskAlgorithms.containsKey(columnRuleConfig.getMaskAlgorithm()), - () -> new AlgorithmNotFoundOnColumnException("mask", columnRuleConfig.getMaskAlgorithm(), databaseName, tableName, columnRuleConfig.getLogicColumn())); + () -> new UnregisteredAlgorithmException("Mask", columnRuleConfig.getMaskAlgorithm(), databaseName, tableName, columnRuleConfig.getLogicColumn())); } @Override diff --git a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/AlgorithmNotFoundOnColumnException.java b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/UnregisteredAlgorithmException.java similarity index 72% rename from infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/AlgorithmNotFoundOnColumnException.java rename to infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/UnregisteredAlgorithmException.java index de3542820cba7..fce0c4a17bdd6 100644 --- a/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/AlgorithmNotFoundOnColumnException.java +++ b/infra/algorithm/core/src/main/java/org/apache/shardingsphere/infra/algorithm/core/exception/UnregisteredAlgorithmException.java @@ -21,14 +21,14 @@ import org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel.category.MetaDataSQLException; /** - * Algorithm not found on column exception. + * Unregistered algorithm exception. */ -public final class AlgorithmNotFoundOnColumnException extends MetaDataSQLException { +public final class UnregisteredAlgorithmException extends MetaDataSQLException { private static final long serialVersionUID = -4570489906443880879L; - public AlgorithmNotFoundOnColumnException(final String algorithmType, final String algorithmName, final String databaseName, final String tableName, final String columnName) { + public UnregisteredAlgorithmException(final String algorithmType, final String algorithmName, final String databaseName, final String tableName, final String columnName) { super(XOpenSQLState.NOT_FOUND, 13, - "Can not find '%s' algorithm '%s' on database.table.column: '%s'.'%s'.'%s'.", algorithmType, algorithmName, databaseName, tableName, columnName); + "'%s' algorithm '%s' on database.table.column: '%s'.'%s'.'%s' is unregistered.", algorithmType, algorithmName, databaseName, tableName, columnName); } }