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

jianglongtao 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 2273dd1  Fixed There is no detailed exception information when adding 
resource is unsuccessful. (#12744)
2273dd1 is described below

commit 2273dd159d9f31b733f57b9cd53ff4732941e77d
Author: lanchengx <[email protected]>
AuthorDate: Sun Sep 26 21:37:01 2021 -0500

    Fixed There is no detailed exception information when adding resource is 
unsuccessful. (#12744)
---
 .../infra/config/datasource/DataSourceValidator.java  |  5 +++--
 .../infra/datasource/DataSourceValidatorTest.java     |  2 +-
 .../rdl/resource/AddResourceBackendHandler.java       | 19 ++++++++++++++-----
 .../rdl/resource/AlterResourceBackendHandler.java     | 19 ++++++++++++++-----
 .../rdl/resource/AddResourceBackendHandlerTest.java   |  2 +-
 .../rdl/resource/AlterResourceBackendHandlerTest.java |  2 +-
 6 files changed, 34 insertions(+), 15 deletions(-)

diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
index c64907a..c4f64e7 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/config/datasource/DataSourceValidator.java
@@ -29,8 +29,9 @@ public final class DataSourceValidator {
      *
      * @param dataSourceConfiguration data source configuration
      * @return is valid or not
+     * @throws Exception exception
      */
-    public boolean validate(final DataSourceConfiguration 
dataSourceConfiguration) {
+    public boolean validate(final DataSourceConfiguration 
dataSourceConfiguration) throws Exception {
         DataSource dataSource = null;
         try {
             dataSource = 
DataSourceConverter.getDataSource(dataSourceConfiguration);
@@ -38,7 +39,7 @@ public final class DataSourceValidator {
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
             // CHECKSTYLE:ON
-            return false;
+            throw ex;
         } finally {
             if (null != dataSource) {
                 close(dataSource);
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
index a98dcce..59ee291 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/datasource/DataSourceValidatorTest.java
@@ -31,7 +31,7 @@ import static org.junit.Assert.assertThat;
 public final class DataSourceValidatorTest {
     
     @Test
-    public void assertValidate() {
+    public void assertValidate() throws Exception {
         DataSourceValidator dataSourceValidator = new DataSourceValidator();
         
assertThat(dataSourceValidator.validate(createDataSourceConfiguration()), 
is(Boolean.TRUE));
     }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
index 34001ae..c6b8854 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandler.java
@@ -39,6 +39,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
 import java.util.Set;
 import java.util.stream.Collectors;
 
@@ -62,11 +63,8 @@ public final class AddResourceBackendHandler extends 
SchemaRequiredBackendHandle
         check(schemaName, sqlStatement);
         Map<String, DataSourceConfiguration> dataSourceConfigs = 
DataSourceParameterConverter.getDataSourceConfigurationMap(
                 
DataSourceParameterConverter.getDataSourceParameterMapFromYamlConfiguration(ResourceSegmentsConverter.convert(databaseType,
 sqlStatement.getDataSources())));
-        Collection<String> invalidDataSourceNames = 
dataSourceConfigs.entrySet()
-                .stream().filter(entry -> 
!dataSourceValidator.validate(entry.getValue())).map(Entry::getKey).collect(Collectors.toList());
-        if (!invalidDataSourceNames.isEmpty()) {
-            throw new InvalidResourceException(invalidDataSourceNames);
-        }
+        Collection<String> invalidResources = 
dataSourceConfigs.entrySet().stream().map(entry -> 
validateDataSource(entry)).filter(Objects::nonNull).collect(Collectors.toList());
+        DistSQLException.predictionThrow(invalidResources.isEmpty(), new 
InvalidResourceException(invalidResources));
         // TODO update meta data context in memory
         ProxyContext.getInstance().getContextManager()
                 
.getMetaDataContexts().getMetaDataPersistService().ifPresent(optional -> 
optional.getDataSourceService().append(schemaName, dataSourceConfigs));
@@ -86,4 +84,15 @@ public final class AddResourceBackendHandler extends 
SchemaRequiredBackendHandle
             throw new DuplicateResourceException(duplicateDataSourceNames);
         }
     }
+    
+    private String validateDataSource(final Entry<String, 
DataSourceConfiguration> dataSource) {
+        try {
+            dataSourceValidator.validate(dataSource.getValue());
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            return String.format("`%s` %s", dataSource.getKey(), 
ex.getMessage());
+        }
+        return null;
+    }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
index 042b711..5d340f0 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandler.java
@@ -38,6 +38,7 @@ import javax.sql.DataSource;
 import java.util.Collection;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.Objects;
 import java.util.stream.Collectors;
 
 /**
@@ -73,12 +74,20 @@ public final class AlterResourceBackendHandler extends 
SchemaRequiredBackendHand
         checkResourceNameExisted(schemaName, toBeAlteredResourceNames);
     }
     
-    private void validate(final Map<String, DataSourceConfiguration> 
dataSourceConfigs) throws InvalidResourceException {
-        Collection<String> invalidDataSourceNames = 
dataSourceConfigs.entrySet()
-                .stream().filter(entry -> 
!dataSourceValidator.validate(entry.getValue())).map(Entry::getKey).collect(Collectors.toList());
-        if (!invalidDataSourceNames.isEmpty()) {
-            throw new InvalidResourceException(invalidDataSourceNames);
+    private void validate(final Map<String, DataSourceConfiguration> 
dataSourceConfigs) throws DistSQLException {
+        Collection<String> invalidResources = 
dataSourceConfigs.entrySet().stream().map(entry -> 
validateDataSource(entry)).filter(Objects::nonNull).collect(Collectors.toList());
+        DistSQLException.predictionThrow(invalidResources.isEmpty(), new 
InvalidResourceException(invalidResources));
+    }
+    
+    private String validateDataSource(final Entry<String, 
DataSourceConfiguration> dataSource) {
+        try {
+            dataSourceValidator.validate(dataSource.getValue());
+            // CHECKSTYLE:OFF
+        } catch (final Exception ex) {
+            // CHECKSTYLE:ON
+            return String.format("`%s` %s", dataSource.getKey(), 
ex.getMessage());
         }
+        return null;
     }
     
     private Collection<String> getToBeAlteredResourceNames(final 
AlterResourceStatement sqlStatement) {
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
index 4ad14b6..c24f0f2 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AddResourceBackendHandlerTest.java
@@ -83,7 +83,7 @@ public final class AddResourceBackendHandlerTest {
     }
     
     @Test
-    public void assertExecute() throws DistSQLException {
+    public void assertExecute() throws Exception {
         ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
         
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
         ProxyContext.getInstance().init(contextManager);
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
index b3dcd81..b6fc597 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/resource/AlterResourceBackendHandlerTest.java
@@ -88,7 +88,7 @@ public final class AlterResourceBackendHandlerTest {
     }
     
     @Test
-    public void assertExecute() throws DistSQLException {
+    public void assertExecute() throws Exception {
         ContextManager contextManager = mock(ContextManager.class, 
RETURNS_DEEP_STUBS);
         
when(contextManager.getMetaDataContexts()).thenReturn(metaDataContexts);
         ProxyContext.getInstance().init(contextManager);

Reply via email to