This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 5b938be7c9c Refactor PostgreSQLErrorPacketFactory and 
OpenGaussErrorPacketFactory (#28130)
5b938be7c9c is described below

commit 5b938be7c9cb8a26340f371f634b393b402b0076
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Aug 17 00:17:36 2023 +0800

    Refactor PostgreSQLErrorPacketFactory and OpenGaussErrorPacketFactory 
(#28130)
---
 .../opengauss/err/OpenGaussErrorPacketFactory.java        | 15 +++++++++------
 .../postgresql/err/PostgreSQLErrorPacketFactory.java      | 14 ++++++++++----
 2 files changed, 19 insertions(+), 10 deletions(-)

diff --git 
a/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/err/OpenGaussErrorPacketFactory.java
 
b/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/err/OpenGaussErrorPacketFactory.java
index e4a792acdac..c15a80c7515 100644
--- 
a/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/err/OpenGaussErrorPacketFactory.java
+++ 
b/proxy/frontend/type/opengauss/src/main/java/org/apache/shardingsphere/proxy/frontend/opengauss/err/OpenGaussErrorPacketFactory.java
@@ -22,15 +22,17 @@ import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import 
org.apache.shardingsphere.db.protocol.opengauss.packet.command.generic.OpenGaussErrorResponsePacket;
 import 
org.apache.shardingsphere.db.protocol.postgresql.constant.PostgreSQLMessageSeverityLevel;
+import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
+import 
org.apache.shardingsphere.infra.exception.core.external.sql.ShardingSphereSQLException;
 import 
org.apache.shardingsphere.infra.exception.dialect.SQLExceptionTransformEngine;
 import 
org.apache.shardingsphere.infra.exception.dialect.exception.SQLDialectException;
 import 
org.apache.shardingsphere.infra.exception.postgresql.vendor.PostgreSQLVendorError;
-import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
-import 
org.apache.shardingsphere.infra.exception.core.external.sql.ShardingSphereSQLException;
 import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 import org.opengauss.util.PSQLException;
+import org.opengauss.util.ServerErrorMessage;
 
 import java.sql.SQLException;
+import java.util.Optional;
 
 /**
  * Error packet factory for openGauss.
@@ -45,8 +47,9 @@ public final class OpenGaussErrorPacketFactory {
      * @return created instance
      */
     public static OpenGaussErrorResponsePacket newInstance(final Exception 
cause) {
-        if (existsServerErrorMessage(cause)) {
-            return new OpenGaussErrorResponsePacket(((PSQLException) 
cause).getServerErrorMessage());
+        Optional<ServerErrorMessage> serverErrorMessage = 
findServerErrorMessage(cause);
+        if (serverErrorMessage.isPresent()) {
+            return new OpenGaussErrorResponsePacket(serverErrorMessage.get());
         }
         if (cause instanceof SQLException || cause instanceof 
ShardingSphereSQLException || cause instanceof SQLDialectException) {
             return 
createErrorResponsePacket(SQLExceptionTransformEngine.toSQLException(cause, 
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")));
@@ -55,8 +58,8 @@ public final class OpenGaussErrorPacketFactory {
         return createErrorResponsePacketForUnknownException(cause);
     }
     
-    private static boolean existsServerErrorMessage(final Exception cause) {
-        return cause instanceof PSQLException && null != ((PSQLException) 
cause).getServerErrorMessage();
+    private static Optional<ServerErrorMessage> findServerErrorMessage(final 
Exception cause) {
+        return cause instanceof PSQLException ? 
Optional.ofNullable(((PSQLException) cause).getServerErrorMessage()) : 
Optional.empty();
     }
     
     private static OpenGaussErrorResponsePacket 
createErrorResponsePacket(final SQLException cause) {
diff --git 
a/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactory.java
 
b/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactory.java
index a0c77958f64..bc914519562 100644
--- 
a/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactory.java
+++ 
b/proxy/frontend/type/postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/err/PostgreSQLErrorPacketFactory.java
@@ -33,6 +33,7 @@ import org.postgresql.util.PSQLException;
 import org.postgresql.util.ServerErrorMessage;
 
 import java.sql.SQLException;
+import java.util.Optional;
 
 /**
  * Error packet factory for PostgreSQL.
@@ -47,8 +48,9 @@ public final class PostgreSQLErrorPacketFactory {
      * @return created instance
      */
     public static PostgreSQLErrorResponsePacket newInstance(final Exception 
cause) {
-        if (cause instanceof PSQLException && null != ((PSQLException) 
cause).getServerErrorMessage()) {
-            return createErrorResponsePacket(((PSQLException) 
cause).getServerErrorMessage());
+        Optional<ServerErrorMessage> serverErrorMessage = 
findServerErrorMessage(cause);
+        if (serverErrorMessage.isPresent()) {
+            return createErrorResponsePacket(serverErrorMessage.get());
         }
         if (cause instanceof SQLException || cause instanceof 
ShardingSphereSQLException || cause instanceof SQLDialectException) {
             return 
createErrorResponsePacket(SQLExceptionTransformEngine.toSQLException(cause, 
TypedSPILoader.getService(DatabaseType.class, "PostgreSQL")));
@@ -57,8 +59,8 @@ public final class PostgreSQLErrorPacketFactory {
         return createErrorResponsePacketForUnknownException(cause);
     }
     
-    private static PostgreSQLErrorResponsePacket 
createErrorResponsePacket(final PostgreSQLException.ServerErrorMessage 
serverErrorMessage) {
-        return 
PostgreSQLErrorResponsePacket.newBuilder(serverErrorMessage.getSeverity(), 
serverErrorMessage.getSqlState(), serverErrorMessage.getMessage()).build();
+    private static Optional<ServerErrorMessage> findServerErrorMessage(final 
Exception cause) {
+        return cause instanceof PSQLException ? 
Optional.ofNullable(((PSQLException) cause).getServerErrorMessage()) : 
Optional.empty();
     }
     
     private static PostgreSQLErrorResponsePacket 
createErrorResponsePacket(final ServerErrorMessage serverErrorMessage) {
@@ -81,6 +83,10 @@ public final class PostgreSQLErrorPacketFactory {
         return 
PostgreSQLErrorResponsePacket.newBuilder(PostgreSQLMessageSeverityLevel.ERROR, 
sqlState, message).build();
     }
     
+    private static PostgreSQLErrorResponsePacket 
createErrorResponsePacket(final PostgreSQLException.ServerErrorMessage 
serverErrorMessage) {
+        return 
PostgreSQLErrorResponsePacket.newBuilder(serverErrorMessage.getSeverity(), 
serverErrorMessage.getSqlState(), serverErrorMessage.getMessage()).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();

Reply via email to