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

panjuan 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 9632d3f1561 Refactor SQLTranslationException (#20446)
9632d3f1561 is described below

commit 9632d3f1561e86676b62535cef01c75b5e74806b
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Aug 23 14:35:01 2022 +0800

    Refactor SQLTranslationException (#20446)
---
 .../content/user-manual/error-code/sql-error-code.cn.md     |  2 ++
 .../content/user-manual/error-code/sql-error-code.en.md     |  2 ++
 .../sqltranslator/exception/SQLTranslationException.java    | 13 ++++++-------
 .../exception/UnsupportedTranslatedDatabaseException.java   |  4 +---
 .../exception/UnsupportedTranslatedSQLException.java        |  6 ++----
 .../sqltranslator/rule/SQLTranslatorRule.java               |  5 ++---
 .../sqltranslator/rule/SQLTranslatorRuleTest.java           |  4 ++--
 .../rule/fixture/AlwaysFailedSQLTranslator.java             |  3 ++-
 .../sqltranslator/jooq/JooQSQLTranslator.java               |  2 +-
 9 files changed, 20 insertions(+), 21 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 cc91cae9d1e..771e277141b 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
@@ -17,6 +17,8 @@ SQL 错误码以标准的 SQL State,Vendor Code 和详细错误信息提供,
 | 42000     | 11001       | configuration error |
 | 42000     | 11002       | Resource does not exist |
 | 42000     | 11003       | Rule does not exist |
+| 42000     | 11200       | Can not support database \`%s\` in SQL translation 
|
+| 42000     | 11201       | Translation error, SQL is: %s |
 | 42000     | 12000       | Unsupported command: %s |
 | 44000     | 13000       | SQL check failed, error message: %s |
 | HY000     | 14000       | The table \`%s\` of schema \`%s\` is locked |
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 04faecbdeed..a0b35218dae 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
@@ -17,6 +17,8 @@ SQL error codes provide by standard `SQL State`, `Vendor 
Code` and `Reason`, whi
 | 42000     | 11001       | configuration error |
 | 42000     | 11002       | Resource does not exist |
 | 42000     | 11003       | Rule does not exist |
+| 42000     | 11200       | Can not support database \`%s\` in SQL translation 
|
+| 42000     | 11201       | Translation error, SQL is: %s |
 | 42000     | 12000       | Unsupported command: %s |
 | 44000     | 13000       | SQL check failed, error message: %s |
 | HY000     | 14000       | The table \`%s\` of schema \`%s\` is locked |
diff --git 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/SQLTranslationException.java
 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/SQLTranslationException.java
index f9d4a93ed51..c8601e26a82 100644
--- 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/SQLTranslationException.java
+++ 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/SQLTranslationException.java
@@ -17,18 +17,17 @@
 
 package org.apache.shardingsphere.sqltranslator.exception;
 
+import 
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
+import 
org.apache.shardingsphere.infra.util.exception.sql.sqlstate.XOpenSQLState;
+
 /**
  * SQL translation exception.
  */
-public class SQLTranslationException extends Exception {
+public abstract class SQLTranslationException extends 
ShardingSphereSQLException {
     
     private static final long serialVersionUID = -7227697280221442049L;
     
-    public SQLTranslationException(final String message) {
-        super(message);
-    }
-    
-    public SQLTranslationException(final String message, final Exception 
cause) {
-        super(message, cause);
+    public SQLTranslationException(final int vendorCode, final String reason, 
final String... messageArguments) {
+        super(XOpenSQLState.SYNTAX_ERROR, vendorCode, reason, 
messageArguments);
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedDatabaseException.java
 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedDatabaseException.java
index a252fbe7dc4..44d32279204 100644
--- 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedDatabaseException.java
+++ 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedDatabaseException.java
@@ -26,9 +26,7 @@ public final class UnsupportedTranslatedDatabaseException 
extends SQLTranslation
     
     private static final long serialVersionUID = -8311552562051028033L;
     
-    private static final String ERROR_MESSAGE = "Can not support database `%s` 
in SQL translation.";
-    
     public UnsupportedTranslatedDatabaseException(final DatabaseType 
databaseType) {
-        super(String.format(ERROR_MESSAGE, databaseType.getType()));
+        super(11200, "Can not support database `%s` in SQL translation", 
databaseType.getType());
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedSQLException.java
 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedSQLException.java
index 98b1e96d5dd..52401d85bd5 100644
--- 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedSQLException.java
+++ 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-api/src/main/java/org/apache/shardingsphere/sqltranslator/exception/UnsupportedTranslatedSQLException.java
@@ -24,9 +24,7 @@ public final class UnsupportedTranslatedSQLException extends 
SQLTranslationExcep
     
     private static final long serialVersionUID = -1419778194546662319L;
     
-    private static final String ERROR_MESSAGE = "SQL `%s` translation error.";
-    
-    public UnsupportedTranslatedSQLException(final String sql, final Exception 
cause) {
-        super(String.format(ERROR_MESSAGE, sql), cause);
+    public UnsupportedTranslatedSQLException(final String sql) {
+        super(11201, "Translation error, SQL is: %s", sql);
     }
 }
diff --git 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java
 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java
index 70f3e58445e..86493b1c85b 100644
--- 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java
+++ 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/main/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRule.java
@@ -19,13 +19,12 @@ package org.apache.shardingsphere.sqltranslator.rule;
 
 import lombok.Getter;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import org.apache.shardingsphere.infra.rule.identifier.scope.GlobalRule;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
 import 
org.apache.shardingsphere.sqltranslator.exception.SQLTranslationException;
-import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
 import org.apache.shardingsphere.sqltranslator.factory.SQLTranslatorFactory;
+import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
 
 /**
  * SQL translator rule.
@@ -64,7 +63,7 @@ public final class SQLTranslatorRule implements GlobalRule {
             if (useOriginalSQLWhenTranslatingFailed) {
                 return sql;
             }
-            throw new ShardingSphereException(ex);
+            throw ex;
         }
     }
     
diff --git 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRuleTest.java
 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRuleTest.java
index 364ac7bc65c..3698c372e0a 100644
--- 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRuleTest.java
+++ 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/SQLTranslatorRuleTest.java
@@ -19,8 +19,8 @@ package org.apache.shardingsphere.sqltranslator.rule;
 
 import org.apache.shardingsphere.infra.database.type.dialect.MySQLDatabaseType;
 import 
org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
-import org.apache.shardingsphere.infra.exception.ShardingSphereException;
 import 
org.apache.shardingsphere.sqltranslator.api.config.SQLTranslatorRuleConfiguration;
+import 
org.apache.shardingsphere.sqltranslator.exception.UnsupportedTranslatedDatabaseException;
 import org.junit.Test;
 
 import java.util.Locale;
@@ -59,7 +59,7 @@ public final class SQLTranslatorRuleTest {
         assertThat(actual, is(expected));
     }
     
-    @Test(expected = ShardingSphereException.class)
+    @Test(expected = UnsupportedTranslatedDatabaseException.class)
     public void assertNotUseOriginalSQLWhenTranslatingFailed() {
         new SQLTranslatorRule(new 
SQLTranslatorRuleConfiguration("ALWAYS_FAILED", false)).translate("", null, new 
PostgreSQLDatabaseType(), new MySQLDatabaseType());
     }
diff --git 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/AlwaysFailedSQLTranslator.java
 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/AlwaysFailedSQLTranslator.java
index ed5e9867fd6..97c5222c80d 100644
--- 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/AlwaysFailedSQLTranslator.java
+++ 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-core/src/test/java/org/apache/shardingsphere/sqltranslator/rule/fixture/AlwaysFailedSQLTranslator.java
@@ -20,13 +20,14 @@ package 
org.apache.shardingsphere.sqltranslator.rule.fixture;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sqltranslator.exception.SQLTranslationException;
+import 
org.apache.shardingsphere.sqltranslator.exception.UnsupportedTranslatedDatabaseException;
 import org.apache.shardingsphere.sqltranslator.spi.SQLTranslator;
 
 public final class AlwaysFailedSQLTranslator implements SQLTranslator {
     
     @Override
     public String translate(final String sql, final SQLStatement sqlStatement, 
final DatabaseType protocolType, final DatabaseType storageType) throws 
SQLTranslationException {
-        throw new SQLTranslationException("Always failed");
+        throw new UnsupportedTranslatedDatabaseException(storageType);
     }
     
     @Override
diff --git 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQSQLTranslator.java
 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQSQLTranslator.java
index a0d0beafe24..4f2e7b8fa38 100644
--- 
a/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQSQLTranslator.java
+++ 
b/shardingsphere-kernel/shardingsphere-sql-translator/shardingsphere-sql-translator-provider/shardingsphere-sql-translator-jooq-provider/src/main/java/org/apache/shardingsphere/sqltranslator/jooq/JooQSQLTranslator.java
@@ -38,7 +38,7 @@ public final class JooQSQLTranslator implements SQLTranslator 
{
             // CHECKSTYLE:OFF
         } catch (final Exception ex) {
             // CHECKSTYLE:ON
-            throw new UnsupportedTranslatedSQLException(sql, ex);
+            throw new UnsupportedTranslatedSQLException(sql);
         }
     }
     

Reply via email to