From 788f5df9958bdc270dd8e5bdb13709c267280ea8 Mon Sep 17 00:00:00 2001 From: Cong Hu Date: Mon, 22 Jul 2024 18:17:57 +0800 Subject: [PATCH] Optimize the message of ShardingSphereSQLException. --- .../external/sql/ShardingSphereSQLException.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 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 8e7d3c64a6234f..8b02a4a38efbb1 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 @@ -18,7 +18,6 @@ package org.apache.shardingsphere.infra.exception.core.external.sql; import com.google.common.base.Preconditions; -import com.google.common.base.Strings; import org.apache.shardingsphere.infra.exception.core.external.ShardingSphereExternalException; import org.apache.shardingsphere.infra.exception.core.external.sql.sqlstate.SQLState; @@ -38,10 +37,6 @@ public abstract class ShardingSphereSQLException extends ShardingSphereExternalE private final int vendorCode; - private final String reason; - - private final Exception cause; - protected ShardingSphereSQLException(final SQLState sqlState, final int typeOffset, final int errorCode, final String reason, final Object... messageArgs) { this(sqlState.getValue(), typeOffset, errorCode, formatMessage(reason, messageArgs), null); } @@ -51,13 +46,15 @@ protected ShardingSphereSQLException(final SQLState sqlState, final int typeOffs } protected ShardingSphereSQLException(final String sqlState, final int typeOffset, final int errorCode, final String reason, final Exception cause) { - super(reason, cause); + super(getMessage(reason, cause), 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 = null == cause || Strings.isNullOrEmpty(cause.getMessage()) ? reason : String.format("%s%sMore details: %s", reason, System.lineSeparator(), cause.getMessage()); - this.cause = cause; + } + + private static String getMessage(final String reason, final Exception cause) { + return null == cause ? reason : String.format("%s%sMore details: %s", reason, System.lineSeparator(), cause); } private static String formatMessage(final String reason, final Object[] messageArgs) { @@ -81,6 +78,6 @@ private static Object[] formatMessageArguments(final Object... messageArgs) { * @return SQL exception */ public final SQLException toSQLException() { - return new SQLException(reason, sqlState, vendorCode, cause); + return new SQLException(getMessage(), sqlState, vendorCode, getCause()); } }