This is an automated email from the ASF dual-hosted git repository.
yx9o 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 7e555d6d4f9 Refactor PostgreSQLErrPacketFactory (#20625)
7e555d6d4f9 is described below
commit 7e555d6d4f93b479b7b23404676117e9663fc86e
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Aug 29 10:59:02 2022 +0800
Refactor PostgreSQLErrPacketFactory (#20625)
---
.../postgresql/err/PostgreSQLErrPacketFactory.java | 28 +++++++++-------------
1 file changed, 11 insertions(+), 17 deletions(-)
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
index 87d73d027a9..ec0d4a444b5 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrPacketFactory.java
@@ -22,8 +22,8 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLMessageSeverityLevel;
import
org.apache.shardingsphere.db.protocol.postgresql.packet.generic.PostgreSQLErrorResponsePacket;
+import org.apache.shardingsphere.dialect.SQLExceptionTransformEngine;
import org.apache.shardingsphere.dialect.exception.SQLDialectException;
-import
org.apache.shardingsphere.dialect.mapper.SQLDialectExceptionMapperFactory;
import
org.apache.shardingsphere.dialect.postgresql.vendor.PostgreSQLVendorError;
import
org.apache.shardingsphere.infra.util.exception.sql.ShardingSphereSQLException;
import
org.apache.shardingsphere.proxy.frontend.postgresql.authentication.exception.InvalidAuthorizationSpecificationException;
@@ -50,15 +50,6 @@ public final class PostgreSQLErrPacketFactory {
if (cause instanceof PSQLException && null != ((PSQLException)
cause).getServerErrorMessage()) {
return createErrorResponsePacket(((PSQLException)
cause).getServerErrorMessage());
}
- if (cause instanceof SQLDialectException) {
- return
createErrorResponsePacket(SQLDialectExceptionMapperFactory.getInstance("PostgreSQL").convert((SQLDialectException)
cause));
- }
- if (cause instanceof ShardingSphereSQLException) {
- return createErrorResponsePacket(((ShardingSphereSQLException)
cause).toSQLException());
- }
- if (cause instanceof SQLException) {
- return createErrorResponsePacket((SQLException) cause);
- }
if (cause instanceof InvalidAuthorizationSpecificationException) {
return
PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.FATAL,
PostgreSQLVendorError.INVALID_AUTHORIZATION_SPECIFICATION,
cause.getMessage()).build();
}
@@ -71,17 +62,13 @@ public final class PostgreSQLErrPacketFactory {
if (cause instanceof PostgreSQLAuthenticationException) {
return
PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.FATAL,
((PostgreSQLAuthenticationException) cause).getVendorError(),
cause.getMessage()).build();
}
+ if (cause instanceof SQLException || cause instanceof
ShardingSphereSQLException || cause instanceof SQLDialectException) {
+ return
createErrorResponsePacket(SQLExceptionTransformEngine.toSQLException(cause,
"PostgreSQL"));
+ }
// TODO PostgreSQL need consider FrontendConnectionLimitException
return createErrorResponsePacketForUnknownException(cause);
}
- private static PostgreSQLErrorResponsePacket
createErrorResponsePacket(final SQLException cause) {
- // TODO consider what severity to use
- String sqlState = Strings.isNullOrEmpty(cause.getSQLState()) ?
PostgreSQLVendorError.SYSTEM_ERROR.getSqlState().getValue() :
cause.getSQLState();
- String message = Strings.isNullOrEmpty(cause.getMessage()) ?
cause.toString() : cause.getMessage();
- return
PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.ERROR,
sqlState, message).build();
- }
-
private static PostgreSQLErrorResponsePacket
createErrorResponsePacket(final ServerErrorMessage serverErrorMessage) {
return
PostgreSQLErrorResponsePacket.newBuilder(serverErrorMessage.getSeverity(),
serverErrorMessage.getSQLState(), serverErrorMessage.getMessage())
.detail(serverErrorMessage.getDetail()).hint(serverErrorMessage.getHint()).position(serverErrorMessage.getPosition())
@@ -90,6 +77,13 @@ public final class PostgreSQLErrPacketFactory {
.constraintName(serverErrorMessage.getConstraint()).file(serverErrorMessage.getFile()).line(serverErrorMessage.getLine()).routine(serverErrorMessage.getRoutine()).build();
}
+ private static PostgreSQLErrorResponsePacket
createErrorResponsePacket(final SQLException cause) {
+ // TODO consider what severity to use
+ String sqlState = Strings.isNullOrEmpty(cause.getSQLState()) ?
PostgreSQLVendorError.SYSTEM_ERROR.getSqlState().getValue() :
cause.getSQLState();
+ String message = Strings.isNullOrEmpty(cause.getMessage()) ?
cause.toString() : cause.getMessage();
+ return
PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.ERROR,
sqlState, message).build();
+ }
+
private static PostgreSQLErrorResponsePacket
createErrorResponsePacketForUnknownException(final Exception cause) {
// TODO add FIELD_TYPE_CODE for common error and consider what
severity to use
String message = Strings.isNullOrEmpty(cause.getLocalizedMessage()) ?
cause.toString() : cause.getLocalizedMessage();