This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 9bb0b751c3b Add typeOffset and errorCode check on
ShardingSphereSQLException (#30685)
9bb0b751c3b is described below
commit 9bb0b751c3b5ac9539290a86e5f69e6e58fd4b4f
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Mar 29 00:30:59 2024 +0800
Add typeOffset and errorCode check on ShardingSphereSQLException (#30685)
* Add typeOffset and errorCode check on ShardingSphereSQLException
* Add typeOffset and errorCode check on KernelSQLException
* Add typeOffset and errorCode check on FeatureSQLException
* Fix test cases
---
.../core/external/sql/ShardingSphereSQLException.java | 3 +++
.../core/external/sql/type/feature/FeatureSQLException.java | 9 ++++++++-
.../core/external/sql/type/kernel/KernelSQLException.java | 11 +++++++++--
3 files changed, 20 insertions(+), 3 deletions(-)
diff --git
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
index cf1b895d45c..758c7323f50 100644
---
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
+++
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/ShardingSphereSQLException.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.infra.exception.core.external.sql;
+import com.google.common.base.Preconditions;
import
org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException;
import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState;
@@ -48,6 +49,8 @@ public abstract class ShardingSphereSQLException extends
ShardingSphereExternalE
protected ShardingSphereSQLException(final String sqlState, final int
typeOffset, final int errorCode, final String reason, final Exception cause) {
super(reason, cause);
this.sqlState = sqlState;
+ Preconditions.checkArgument(typeOffset >= 0 && typeOffset < 4, "The
value range of type offset should be [0, 3].");
+ Preconditions.checkArgument(errorCode >= 0 && errorCode < 10000, "The
value range of error code should be [0, 10000).");
vendorCode = typeOffset * 10000 + errorCode;
this.reason = reason;
this.cause = cause;
diff --git
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/feature/FeatureSQLException.java
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/feature/FeatureSQLException.java
index dc5230327b1..340003b3ed3 100644
---
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/feature/FeatureSQLException.java
+++
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/feature/FeatureSQLException.java
@@ -17,6 +17,7 @@
package
org.apache.shardingsphere.infra.exception.core.external.sql.type.feature;
+import com.google.common.base.Preconditions;
import
org.apache.shardingsphere.infra.exception.core.external.sql.ShardingSphereSQLException;
import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState;
@@ -30,6 +31,12 @@ public abstract class FeatureSQLException extends
ShardingSphereSQLException {
private static final int TYPE_OFFSET = 2;
protected FeatureSQLException(final SQLState sqlState, final int
featureCode, final int errorCode, final String reason, final Object...
messageArgs) {
- super(sqlState, TYPE_OFFSET, featureCode * 100 + errorCode, reason,
messageArgs);
+ super(sqlState, TYPE_OFFSET, getErrorCode(featureCode, errorCode),
reason, messageArgs);
+ }
+
+ private static int getErrorCode(final int featureCode, final int
errorCode) {
+ Preconditions.checkArgument(featureCode >= 0 && featureCode < 100,
"The value range of feature code should be [0, 100).");
+ Preconditions.checkArgument(errorCode >= 0 && errorCode < 100, "The
value range of error code should be [0, 100).");
+ return featureCode * 100 + errorCode;
}
}
diff --git
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLException.java
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLException.java
index 078fcb2f10b..c4db8e52b51 100644
---
a/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLException.java
+++
b/infra/exception/core/src/main/java/org/apache/shardingsphere/infra/exception/core/external/sql/type/kernel/KernelSQLException.java
@@ -17,6 +17,7 @@
package
org.apache.shardingsphere.infra.exception.core.external.sql.type.kernel;
+import com.google.common.base.Preconditions;
import
org.apache.shardingsphere.infra.exception.core.external.sql.ShardingSphereSQLException;
import
org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState;
@@ -30,10 +31,16 @@ public abstract class KernelSQLException extends
ShardingSphereSQLException {
private static final int TYPE_OFFSET = 1;
protected KernelSQLException(final SQLState sqlState, final int
kernelCode, final int errorCode, final String reason, final Object...
messageArgs) {
- super(sqlState, TYPE_OFFSET, kernelCode * 1000 + errorCode, reason,
messageArgs);
+ super(sqlState, TYPE_OFFSET, getErrorCode(kernelCode, errorCode),
reason, messageArgs);
}
protected KernelSQLException(final SQLState sqlState, final int
kernelCode, final int errorCode, final String reason, final Exception cause) {
- super(sqlState.getValue(), TYPE_OFFSET, kernelCode * 1000 + errorCode,
reason, cause);
+ super(sqlState.getValue(), TYPE_OFFSET, getErrorCode(kernelCode,
errorCode), reason, cause);
+ }
+
+ private static int getErrorCode(final int kernelCode, final int errorCode)
{
+ Preconditions.checkArgument(kernelCode >= 0 && kernelCode < 10, "The
value range of kernel code should be [0, 10).");
+ Preconditions.checkArgument(errorCode >= 0 && errorCode < 1000, "The
value range of error code should be [0, 1000).");
+ return kernelCode * 1000 + errorCode;
}
}