Skip to content

Commit

Permalink
Optimize the message of ShardingSphereSQLException.
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhucong committed Jul 22, 2024
1 parent 45b69b4 commit 788f5df
Showing 1 changed file with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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());
}
}

0 comments on commit 788f5df

Please sign in to comment.