You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2026/09/19 05:26:59 UTC

(shardingsphere) branch master updated: Fix rule configuration validation error handling (#39894)

This is an automated email from the ASF dual-hosted git repository.

terrymanu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 956868bd8b2 Fix rule configuration validation error handling (#39894)
956868bd8b2 is described below

commit 956868bd8b29547a0474e02aaaa0bd9aa87d6393
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sat Sep 19 13:26:51 2026 +0800

    Fix rule configuration validation error handling (#39894)
    
    * Remove redundant encryptor validation from create executor
    
    * Fix rule configuration validation error handling
    
    * Fix rule configuration validation error handling
---
 .../user-manual/error-code/sql-error-code.cn.md    |  1 +
 .../user-manual/error-code/sql-error-code.en.md    |  1 +
 .../rule/validator/RuleConfigurationValidator.java |  9 +++++++-
 .../constraint/spi/SPITypeExistsValidator.java     |  4 ++--
 ...a => RuleConfigurationValidationException.java} | 21 +++++------------
 .../metadata/rule/RuleDefinitionException.java     |  4 ++++
 .../validator/RuleConfigurationValidatorTest.java  | 27 ++++++++++++++++++++++
 .../constraint/spi/SPITypeExistsValidatorTest.java |  8 +++++--
 8 files changed, 55 insertions(+), 20 deletions(-)

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 c32042360ae..c29ba1350f2 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
@@ -43,6 +43,7 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | 10202       | 42S02     | %s rules '%s' do not exist in database '%s'.                                        |
 | 10203       | 44000     | %s rules '%s' in database '%s' are still in used by %s.                             |
 | 10204       | 42S01     | Duplicate %s rule names '%s' in database '%s'.                                      |
+| 10205       | HY000     | Can not validate '%s' rule configuration.                                           |
 | 10210       | 42S02     | %s strategies '%s' do not exist.                                                    |
 | 10300       | HY000     | Invalid format for data node '%s', reason is: %s.                                   |
 | 10301       | 0A000     | Can not support 3-tier structure for actual data node '%s.%s' with JDBC '%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 6adec94bdf0..dae92ada4f9 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
@@ -43,6 +43,7 @@ SQL error codes provide by standard `SQL State`, `Vendor Code` and `Reason`, whi
 | 10202       | 42S02     | %s rules '%s' do not exist in database '%s'.                                        |
 | 10203       | 44000     | %s rules '%s' in database '%s' are still in used by %s.                             |
 | 10204       | 42S01     | Duplicate %s rule names '%s' in database '%s'.                                      |
+| 10205       | HY000     | Can not validate '%s' rule configuration.                                           |
 | 10210       | 42S02     | %s strategies '%s' do not exist.                                                    |
 | 10300       | HY000     | Invalid format for data node '%s', reason is: %s.                                   |
 | 10301       | 0A000     | Can not support 3-tier structure for actual data node '%s.%s' with JDBC '%s'.       |
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/RuleConfigurationValidator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/RuleConfigurationValidator.java
index 82ee28043f3..7f24c58538e 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/RuleConfigurationValidator.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/RuleConfigurationValidator.java
@@ -23,9 +23,11 @@ import org.apache.bval.jsr.ApacheValidationProvider;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException;
+import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.RuleConfigurationValidationException;
 
 import javax.validation.ConstraintViolation;
 import javax.validation.Validation;
+import javax.validation.ValidationException;
 import javax.validation.Validator;
 import java.util.Collection;
 import java.util.Set;
@@ -60,7 +62,12 @@ public final class RuleConfigurationValidator {
     }
     
     private static void validate(final Object config, final String ruleType) {
-        Set<ConstraintViolation<Object>> violations = VALIDATOR.validate(config);
+        Set<ConstraintViolation<Object>> violations;
+        try {
+            violations = VALIDATOR.validate(config);
+        } catch (final ValidationException ex) {
+            throw new RuleConfigurationValidationException(ruleType, ex);
+        }
         ShardingSpherePreconditions.checkMustEmpty(violations, () -> new InvalidRuleConfigurationException(
                 ruleType, violations.stream().map(RuleConfigurationValidator::formatViolation).sorted().collect(Collectors.joining("; "))));
     }
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidator.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidator.java
index ac45da03fb9..41eb835acef 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidator.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidator.java
@@ -59,8 +59,8 @@ public final class SPITypeExistsValidator implements ConstraintValidator<SPIType
             Class<?> spiClass = Class.forName(spiClassName, false, Thread.currentThread().getContextClassLoader());
             ShardingSpherePreconditions.checkState(TypedSPI.class.isAssignableFrom(spiClass), () -> new ValidationException(String.format("Class `%s` does not implement TypedSPI.", spiClassName)));
             return TypedSPILoader.containsService(spiClass.asSubclass(TypedSPI.class), type);
-        } catch (final ClassNotFoundException ignored) {
-            return false;
+        } catch (final ClassNotFoundException ex) {
+            throw new ValidationException(String.format("Can not load SPI class `%s`.", spiClassName), ex);
         }
     }
     
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleDefinitionException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleConfigurationValidationException.java
similarity index 53%
copy from infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleDefinitionException.java
copy to infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleConfigurationValidationException.java
index 2aadec139c5..13308905f77 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleDefinitionException.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleConfigurationValidationException.java
@@ -17,25 +17,16 @@
 
 package org.apache.shardingsphere.infra.exception.kernel.metadata.rule;
 
-import com.google.common.base.Preconditions;
-import org.apache.shardingsphere.infra.exception.external.sql.sqlstate.SQLState;
-import org.apache.shardingsphere.infra.exception.external.sql.type.kernel.category.MetaDataSQLException;
+import org.apache.shardingsphere.infra.exception.external.sql.sqlstate.XOpenSQLState;
 
 /**
- * Rule definition exception.
+ * Rule configuration validation exception.
  */
-public abstract class RuleDefinitionException extends MetaDataSQLException {
+public final class RuleConfigurationValidationException extends RuleDefinitionException {
     
-    private static final long serialVersionUID = -6414242067345718028L;
+    private static final long serialVersionUID = -4109547431930113883L;
     
-    private static final int RULE_CODE = 2;
-    
-    protected RuleDefinitionException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArgs) {
-        super(sqlState, getErrorCode(errorCode), reason, messageArgs);
-    }
-    
-    private static int getErrorCode(final int errorCode) {
-        Preconditions.checkArgument(errorCode >= 0 && errorCode < 100, "The value range of error code should be [0, 100).");
-        return RULE_CODE * 100 + errorCode;
+    public RuleConfigurationValidationException(final String ruleType, final Exception cause) {
+        super(XOpenSQLState.GENERAL_ERROR, 5, cause, "Can not validate '%s' rule configuration.", ruleType);
     }
 }
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleDefinitionException.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleDefinitionException.java
index 2aadec139c5..edbde583653 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleDefinitionException.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/exception/kernel/metadata/rule/RuleDefinitionException.java
@@ -34,6 +34,10 @@ public abstract class RuleDefinitionException extends MetaDataSQLException {
         super(sqlState, getErrorCode(errorCode), reason, messageArgs);
     }
     
+    protected RuleDefinitionException(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) {
         Preconditions.checkArgument(errorCode >= 0 && errorCode < 100, "The value range of error code should be [0, 100).");
         return RULE_CODE * 100 + errorCode;
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/RuleConfigurationValidatorTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/RuleConfigurationValidatorTest.java
index 3f945fb9031..c7cc95f5fdf 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/RuleConfigurationValidatorTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/RuleConfigurationValidatorTest.java
@@ -20,15 +20,22 @@ package org.apache.shardingsphere.infra.config.rule.validator;
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.SPITypeExists;
+import org.apache.shardingsphere.infra.exception.external.sql.sqlstate.XOpenSQLState;
 import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.InvalidRuleConfigurationException;
+import org.apache.shardingsphere.infra.exception.kernel.metadata.rule.RuleConfigurationValidationException;
 import org.junit.jupiter.api.Test;
 
 import javax.validation.Valid;
+import javax.validation.ValidationException;
 import javax.validation.constraints.NotBlank;
+import java.sql.SQLException;
 import java.util.Arrays;
 
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.isA;
 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
@@ -45,6 +52,9 @@ class RuleConfigurationValidatorTest {
                 () -> RuleConfigurationValidator.validate(new FixtureRuleConfiguration("")));
         assertThat(actual.getMessage(), containsString("Invalid 'FixtureRuleConfiguration' rule"));
         assertThat(actual.getMessage(), containsString("Property `nested.name`"));
+        SQLException actualSQLException = actual.toSQLException();
+        assertThat(actualSQLException.getSQLState(), is(XOpenSQLState.CHECK_OPTION_VIOLATION.getValue()));
+        assertThat(actualSQLException.getErrorCode(), is(10200));
     }
     
     @Test
@@ -61,6 +71,16 @@ class RuleConfigurationValidatorTest {
         assertThat(actual.getMessage(), containsString("Property `name`"));
     }
     
+    @Test
+    void assertValidateRuleConfigurationWithValidationException() {
+        RuleConfigurationValidationException actual = assertThrows(RuleConfigurationValidationException.class,
+                () -> RuleConfigurationValidator.validate(new MissingSPIClassRuleConfiguration()));
+        assertThat(actual.getCause(), isA(ValidationException.class));
+        SQLException actualSQLException = actual.toSQLException();
+        assertThat(actualSQLException.getSQLState(), is(XOpenSQLState.GENERAL_ERROR.getValue()));
+        assertThat(actualSQLException.getErrorCode(), is(10205));
+    }
+    
     @Getter
     private static final class FixtureRuleConfiguration implements RuleConfiguration {
         
@@ -79,4 +99,11 @@ class RuleConfigurationValidatorTest {
         @NotBlank
         private final String name;
     }
+    
+    @Getter
+    private static final class MissingSPIClassRuleConfiguration implements RuleConfiguration {
+        
+        @SPITypeExists(spiClassName = "org.apache.shardingsphere.infra.config.rule.validator.constraint.spi.fixture.MissingSPITypeFixture")
+        private final String type = "FIXTURE";
+    }
 }
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidatorTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidatorTest.java
index a5b5c2c3c0e..4d59ba87122 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidatorTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/config/rule/validator/constraint/spi/SPITypeExistsValidatorTest.java
@@ -61,8 +61,7 @@ class SPITypeExistsValidatorTest {
                 Arguments.of("Empty map", new MapConfiguration(Collections.emptyMap()), true),
                 Arguments.of("Map type", new MapConfiguration(Collections.singletonMap("foo", new SPIConfiguration("FIXTURE"))), true),
                 Arguments.of("Missing map type", new MapConfiguration(Collections.singletonMap("foo", new SPIConfiguration("MISSING"))), false),
-                Arguments.of("Null map value", new MapConfiguration(Collections.singletonMap("foo", null)), true),
-                Arguments.of("Missing SPI class", new MissingSPIClassConfiguration("FIXTURE"), false));
+                Arguments.of("Null map value", new MapConfiguration(Collections.singletonMap("foo", null)), true));
     }
     
     @Test
@@ -76,6 +75,11 @@ class SPITypeExistsValidatorTest {
         assertThrows(ValidationException.class, () -> VALIDATOR.validate(new InvalidSPIClassConfiguration("FIXTURE")));
     }
     
+    @Test
+    void assertMissingSPIClass() {
+        assertThrows(ValidationException.class, () -> VALIDATOR.validate(new MissingSPIClassConfiguration("FIXTURE")));
+    }
+    
     @Test
     void assertMissingTypeGetter() {
         assertThrows(ValidationException.class, () -> VALIDATOR.validate(new MissingTypeGetterConfiguration(new Object())));