This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 cac2528878e Refactor SQLExceptionHandler (#20165)
cac2528878e is described below
commit cac2528878e1da6679b61bdbbae034dd98624ce1
Author: Liang Zhang <[email protected]>
AuthorDate: Sun Aug 14 15:34:34 2022 +0800
Refactor SQLExceptionHandler (#20165)
---
.../shardingsphere/error/SQLExceptionHandler.java | 38 ++++------------------
.../standard/ShardingSphereSQLException.java | 25 ++++++++++++++
.../command/UnsupportedCommandException.java | 9 +++--
.../standard/connection/CircuitBreakException.java | 5 +++
.../lock/TableLockWaitTimeoutException.java | 13 +++-----
.../standard/lock/TableLockedException.java | 11 +++----
.../standard/rule/ResourceNotExistedException.java | 5 +++
.../standard/rule/RuleNotExistedException.java | 5 +++
.../exception/UnsupportedCommandExceptionTest.java | 32 ------------------
9 files changed, 59 insertions(+), 84 deletions(-)
diff --git
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/SQLExceptionHandler.java
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/SQLExceptionHandler.java
index 01724ddcf50..007b11a61e4 100644
---
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/SQLExceptionHandler.java
+++
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/SQLExceptionHandler.java
@@ -20,12 +20,7 @@ package org.apache.shardingsphere.error;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.error.exception.dialect.SQLDialectException;
-import
org.apache.shardingsphere.error.exception.standard.command.UnsupportedCommandException;
-import
org.apache.shardingsphere.error.exception.standard.connection.CircuitBreakException;
-import
org.apache.shardingsphere.error.exception.standard.lock.TableLockWaitTimeoutException;
-import
org.apache.shardingsphere.error.exception.standard.lock.TableLockedException;
-import
org.apache.shardingsphere.error.exception.standard.rule.ResourceNotExistedException;
-import
org.apache.shardingsphere.error.exception.standard.rule.RuleNotExistedException;
+import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLException;
import org.apache.shardingsphere.error.mapper.SQLDialectExceptionMapperFactory;
import org.apache.shardingsphere.error.vendor.ShardingSphereVendorError;
import org.apache.shardingsphere.error.vendor.VendorError;
@@ -34,7 +29,6 @@ import
org.apache.shardingsphere.infra.util.exception.ShardingSphereInsideExcept
import org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
import java.sql.SQLException;
-import java.util.Optional;
/**
* SQL exception handler.
@@ -53,34 +47,16 @@ public final class SQLExceptionHandler {
if (insideException instanceof SQLDialectException) {
return
SQLDialectExceptionMapperFactory.getInstance(databaseType).convert((SQLDialectException)
insideException);
}
- return convert(insideException).orElseGet(() ->
toSQLException(ShardingSphereVendorError.UNKNOWN_EXCEPTION,
insideException.getMessage()));
- }
-
- private static Optional<SQLException> convert(final
ShardingSphereInsideException insideException) {
- if (insideException instanceof CircuitBreakException) {
- return
Optional.of(toSQLException(ShardingSphereVendorError.CIRCUIT_BREAK_MODE));
- }
- if (insideException instanceof TableLockWaitTimeoutException) {
- TableLockWaitTimeoutException exception =
(TableLockWaitTimeoutException) insideException;
- return
Optional.of(toSQLException(ShardingSphereVendorError.TABLE_LOCK_WAIT_TIMEOUT,
exception.getTableName(), exception.getSchemaName(),
exception.getTimeoutMilliseconds()));
- }
- if (insideException instanceof TableLockedException) {
- TableLockedException exception = (TableLockedException)
insideException;
- return
Optional.of(toSQLException(ShardingSphereVendorError.TABLE_LOCKED,
exception.getTableName(), exception.getSchemaName()));
- }
- if (insideException instanceof RuleNotExistedException ||
insideException instanceof ResourceNotExistedException) {
- return
Optional.of(toSQLException(ShardingSphereVendorError.RESOURCE_OR_RULE_NOT_EXIST));
+ if (insideException instanceof ShardingSphereSQLException) {
+ return ((ShardingSphereSQLException)
insideException).toSQLException();
}
if (insideException instanceof ShardingSphereConfigurationException ||
insideException instanceof SQLParsingException) {
- return
Optional.of(toSQLException(ShardingSphereVendorError.UNSUPPORTED_SQL,
insideException.getMessage()));
- }
- if (insideException instanceof UnsupportedCommandException) {
- return
Optional.of(toSQLException(ShardingSphereVendorError.UNSUPPORTED_COMMAND,
((UnsupportedCommandException) insideException).getCommandType()));
+ return toSQLException(ShardingSphereVendorError.UNSUPPORTED_SQL,
insideException);
}
- return Optional.empty();
+ return toSQLException(ShardingSphereVendorError.UNKNOWN_EXCEPTION,
insideException);
}
- private static SQLException toSQLException(final VendorError vendorError,
final Object... messageArguments) {
- return new SQLException(String.format(vendorError.getReason(),
messageArguments), vendorError.getSqlState().getValue(),
vendorError.getVendorCode());
+ private static SQLException toSQLException(final VendorError vendorError,
final ShardingSphereInsideException insideException) {
+ return new SQLException(String.format(vendorError.getReason(),
insideException.getMessage()), vendorError.getSqlState().getValue(),
vendorError.getVendorCode());
}
}
diff --git
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/ShardingSphereSQLException.java
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/ShardingSphereSQLException.java
index d9c895f1bc0..d9c093eece0 100644
---
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/ShardingSphereSQLException.java
+++
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/ShardingSphereSQLException.java
@@ -17,12 +17,37 @@
package org.apache.shardingsphere.error.exception.standard;
+import org.apache.shardingsphere.error.sqlstate.SQLState;
import
org.apache.shardingsphere.infra.util.exception.ShardingSphereInsideException;
+import java.sql.SQLException;
+
/**
* ShardingSphere SQL exception.
*/
public abstract class ShardingSphereSQLException extends
ShardingSphereInsideException {
private static final long serialVersionUID = -8238061892944243621L;
+
+ private final SQLState sqlState;
+
+ private final int vendorCode;
+
+ private final String reason;
+
+ @SuppressWarnings("ConfusingArgumentToVarargsMethod")
+ public ShardingSphereSQLException(final SQLState sqlState, final int
vendorCode, final String reason, final String... messageArguments) {
+ this.sqlState = sqlState;
+ this.vendorCode = vendorCode;
+ this.reason = String.format(reason, messageArguments);
+ }
+
+ /**
+ * To SQL exception.
+ *
+ * @return SQL exception
+ */
+ public final SQLException toSQLException() {
+ return new SQLException(reason, sqlState.getValue(), vendorCode);
+ }
}
diff --git
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/command/UnsupportedCommandException.java
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/command/UnsupportedCommandException.java
index 84773f64231..122bbb7bb33 100644
---
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/command/UnsupportedCommandException.java
+++
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/command/UnsupportedCommandException.java
@@ -17,18 +17,17 @@
package org.apache.shardingsphere.error.exception.standard.command;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLException;
+import org.apache.shardingsphere.error.sqlstate.XOpenSQLState;
/**
* Unsupported command exception.
*/
-@RequiredArgsConstructor
-@Getter
public final class UnsupportedCommandException extends
ShardingSphereSQLException {
private static final long serialVersionUID = 8010680371699936338L;
- private final String commandType;
+ public UnsupportedCommandException(final String commandType) {
+ super(XOpenSQLState.SYNTAX_ERROR, 1998, "Unsupported command: %s",
commandType);
+ }
}
diff --git
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/connection/CircuitBreakException.java
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/connection/CircuitBreakException.java
index 5d7f2d8ae43..a7a36f7cccd 100644
---
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/connection/CircuitBreakException.java
+++
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/connection/CircuitBreakException.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.error.exception.standard.connection;
import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLException;
+import org.apache.shardingsphere.error.sqlstate.XOpenSQLState;
/**
* Circuit break exception.
@@ -25,4 +26,8 @@ import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLExcep
public final class CircuitBreakException extends ShardingSphereSQLException {
private static final long serialVersionUID = 6339672680026286798L;
+
+ public CircuitBreakException() {
+ super(XOpenSQLState.GENERAL_WARNING, 1000, "Circuit break open, the
request has been ignored");
+ }
}
diff --git
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/lock/TableLockWaitTimeoutException.java
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/lock/TableLockWaitTimeoutException.java
index 43b9a0d1f3c..e1dbcdf849b 100644
---
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/lock/TableLockWaitTimeoutException.java
+++
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/lock/TableLockWaitTimeoutException.java
@@ -17,22 +17,17 @@
package org.apache.shardingsphere.error.exception.standard.lock;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLException;
+import org.apache.shardingsphere.error.sqlstate.XOpenSQLState;
/**
* Table lock wait timeout exception.
*/
-@RequiredArgsConstructor
-@Getter
public final class TableLockWaitTimeoutException extends
ShardingSphereSQLException {
private static final long serialVersionUID = 2599713085782288003L;
- private final String schemaName;
-
- private final String tableName;
-
- private final long timeoutMilliseconds;
+ public TableLockWaitTimeoutException(final String schemaName, final String
tableName, final long timeoutMilliseconds) {
+ super(XOpenSQLState.GENERAL_ERROR, 1301, "The table `%s` of schema
`%s` lock wait timeout of %s ms exceeded", tableName, schemaName,
String.valueOf(timeoutMilliseconds));
+ }
}
diff --git
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/lock/TableLockedException.java
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/lock/TableLockedException.java
index 301ad918246..bfcea525542 100644
---
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/lock/TableLockedException.java
+++
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/lock/TableLockedException.java
@@ -17,20 +17,17 @@
package org.apache.shardingsphere.error.exception.standard.lock;
-import lombok.Getter;
-import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLException;
+import org.apache.shardingsphere.error.sqlstate.XOpenSQLState;
/**
* Table locked exception.
*/
-@RequiredArgsConstructor
-@Getter
public final class TableLockedException extends ShardingSphereSQLException {
private static final long serialVersionUID = 2622020743612706932L;
- private final String schemaName;
-
- private final String tableName;
+ public TableLockedException(final String schemaName, final String
tableName) {
+ super(XOpenSQLState.GENERAL_ERROR, 1302, "The table `%s` of schema
`%s` is locked", tableName, schemaName);
+ }
}
diff --git
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/rule/ResourceNotExistedException.java
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/rule/ResourceNotExistedException.java
index 3b6f03f2731..d0bf758a559 100644
---
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/rule/ResourceNotExistedException.java
+++
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/rule/ResourceNotExistedException.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.error.exception.standard.rule;
import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLException;
+import org.apache.shardingsphere.error.sqlstate.XOpenSQLState;
/**
* Resource does not exist exception.
@@ -25,4 +26,8 @@ import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLExcep
public final class ResourceNotExistedException extends
ShardingSphereSQLException {
private static final long serialVersionUID = 4146100333670404924L;
+
+ public ResourceNotExistedException() {
+ super(XOpenSQLState.SYNTAX_ERROR, 1305, "Data source or rule does not
exist");
+ }
}
diff --git
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/rule/RuleNotExistedException.java
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/rule/RuleNotExistedException.java
index f5bf441607b..01e25af710d 100644
---
a/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/rule/RuleNotExistedException.java
+++
b/shardingsphere-error/shardingsphere-common-error/src/main/java/org/apache/shardingsphere/error/exception/standard/rule/RuleNotExistedException.java
@@ -18,6 +18,7 @@
package org.apache.shardingsphere.error.exception.standard.rule;
import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLException;
+import org.apache.shardingsphere.error.sqlstate.XOpenSQLState;
/**
* Rule does not exist exception.
@@ -25,4 +26,8 @@ import
org.apache.shardingsphere.error.exception.standard.ShardingSphereSQLExcep
public final class RuleNotExistedException extends ShardingSphereSQLException {
private static final long serialVersionUID = -4150905802300104824L;
+
+ public RuleNotExistedException() {
+ super(XOpenSQLState.SYNTAX_ERROR, 1305, "Data source or rule does not
exist");
+ }
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/exception/UnsupportedCommandExceptionTest.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/exception/UnsupportedCommandExceptionTest.java
deleted file mode 100644
index 76706c1d786..00000000000
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/test/java/org/apache/shardingsphere/proxy/frontend/exception/UnsupportedCommandExceptionTest.java
+++ /dev/null
@@ -1,32 +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.proxy.frontend.exception;
-
-import
org.apache.shardingsphere.error.exception.standard.command.UnsupportedCommandException;
-import org.junit.Test;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-public final class UnsupportedCommandExceptionTest {
-
- @Test
- public void assertNewInstanceAndGetCommandType() {
- assertThat(new
UnsupportedCommandException("unsupported").getCommandType(), is("unsupported"));
- }
-}