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

yuqi4733 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/gravitino.git


The following commit(s) were added to refs/heads/main by this push:
     new 3dd59a8c5 [#4735] improvement(core): Optimize the thrown duplicated 
entry error message (#4838)
3dd59a8c5 is described below

commit 3dd59a8c59ede2774123fe96eb989adec13c3da3
Author: xloya <[email protected]>
AuthorDate: Mon Sep 2 21:45:58 2024 +0800

    [#4735] improvement(core): Optimize the thrown duplicated entry error 
message (#4838)
    
    ### What changes were proposed in this pull request?
    
    Optimize the exception message of `SQLException` thrown in UI when
    duplicate entities occurs.
    
    
![image](https://github.com/user-attachments/assets/df74c64e-097a-4845-9ec7-cf914934b26d)
    
    ### Why are the changes needed?
    
    Fix: #4735
    
    ### How was this patch tested?
    
    Add some UTs.
---
 .../converters/H2ExceptionConverter.java           |  3 +-
 .../converters/MySQLExceptionConverter.java        |  3 +-
 .../converters/TestH2ExceptionConverter.java}      | 32 +++++++++-------------
 .../converters/TestMySQLExceptionConverter.java}   | 31 +++++++++------------
 4 files changed, 30 insertions(+), 39 deletions(-)

diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
index 462fc6a14..a64db131a 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
@@ -37,7 +37,8 @@ public class H2ExceptionConverter implements 
SQLExceptionConverter {
       throws IOException {
     switch (se.getErrorCode()) {
       case DUPLICATED_ENTRY_ERROR_CODE:
-        throw new EntityAlreadyExistsException(se, se.getMessage());
+        throw new EntityAlreadyExistsException(
+            se, "The %s entity: %s already exists.", type.name(), name);
       default:
         throw new IOException(se);
     }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/MySQLExceptionConverter.java
 
b/core/src/main/java/org/apache/gravitino/storage/relational/converters/MySQLExceptionConverter.java
index 2d9f34e10..1190f9ce4 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/MySQLExceptionConverter.java
+++ 
b/core/src/main/java/org/apache/gravitino/storage/relational/converters/MySQLExceptionConverter.java
@@ -38,7 +38,8 @@ public class MySQLExceptionConverter implements 
SQLExceptionConverter {
       throws IOException {
     switch (se.getErrorCode()) {
       case DUPLICATED_ENTRY_ERROR_CODE:
-        throw new EntityAlreadyExistsException(se, se.getMessage());
+        throw new EntityAlreadyExistsException(
+            se, "The %s entity: %s already exists.", type.name(), name);
       default:
         throw new IOException(se);
     }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
 
b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestH2ExceptionConverter.java
similarity index 55%
copy from 
core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
copy to 
core/src/test/java/org/apache/gravitino/storage/relational/converters/TestH2ExceptionConverter.java
index 462fc6a14..f07abd3b5 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
+++ 
b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestH2ExceptionConverter.java
@@ -18,28 +18,22 @@
  */
 package org.apache.gravitino.storage.relational.converters;
 
-import java.io.IOException;
 import java.sql.SQLException;
 import org.apache.gravitino.Entity;
 import org.apache.gravitino.EntityAlreadyExistsException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
-/**
- * Exception converter to Apache Gravitino exception for H2. The definition of 
error codes can be
- * found in the document: <a 
href="https://h2database.com/javadoc/org/h2/api/ErrorCode.html";></a>
- */
-public class H2ExceptionConverter implements SQLExceptionConverter {
-  /** It means found a duplicated primary key or unique key entry in H2. */
-  private static final int DUPLICATED_ENTRY_ERROR_CODE = 23505;
-
-  @SuppressWarnings("FormatStringAnnotation")
-  @Override
-  public void toGravitinoException(SQLException se, Entity.EntityType type, 
String name)
-      throws IOException {
-    switch (se.getErrorCode()) {
-      case DUPLICATED_ENTRY_ERROR_CODE:
-        throw new EntityAlreadyExistsException(se, se.getMessage());
-      default:
-        throw new IOException(se);
-    }
+public class TestH2ExceptionConverter {
+  @Test
+  public void testConvertDuplicatedEntryException() {
+    SQLException mockException = Mockito.mock(SQLException.class);
+    Mockito.when(mockException.getErrorCode()).thenReturn(23505);
+    H2ExceptionConverter converter = new H2ExceptionConverter();
+    Assertions.assertThrows(
+        EntityAlreadyExistsException.class,
+        () -> converter.toGravitinoException(mockException, 
Entity.EntityType.METALAKE, "test"),
+        String.format("The %s entity: %s already exists.", 
Entity.EntityType.METALAKE, "test"));
   }
 }
diff --git 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
 
b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestMySQLExceptionConverter.java
similarity index 55%
copy from 
core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
copy to 
core/src/test/java/org/apache/gravitino/storage/relational/converters/TestMySQLExceptionConverter.java
index 462fc6a14..9a94db6e1 100644
--- 
a/core/src/main/java/org/apache/gravitino/storage/relational/converters/H2ExceptionConverter.java
+++ 
b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestMySQLExceptionConverter.java
@@ -18,28 +18,23 @@
  */
 package org.apache.gravitino.storage.relational.converters;
 
-import java.io.IOException;
 import java.sql.SQLException;
 import org.apache.gravitino.Entity;
 import org.apache.gravitino.EntityAlreadyExistsException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
 
-/**
- * Exception converter to Apache Gravitino exception for H2. The definition of 
error codes can be
- * found in the document: <a 
href="https://h2database.com/javadoc/org/h2/api/ErrorCode.html";></a>
- */
-public class H2ExceptionConverter implements SQLExceptionConverter {
-  /** It means found a duplicated primary key or unique key entry in H2. */
-  private static final int DUPLICATED_ENTRY_ERROR_CODE = 23505;
+public class TestMySQLExceptionConverter {
 
-  @SuppressWarnings("FormatStringAnnotation")
-  @Override
-  public void toGravitinoException(SQLException se, Entity.EntityType type, 
String name)
-      throws IOException {
-    switch (se.getErrorCode()) {
-      case DUPLICATED_ENTRY_ERROR_CODE:
-        throw new EntityAlreadyExistsException(se, se.getMessage());
-      default:
-        throw new IOException(se);
-    }
+  @Test
+  public void testConvertDuplicatedEntryException() {
+    SQLException mockException = Mockito.mock(SQLException.class);
+    Mockito.when(mockException.getErrorCode()).thenReturn(1062);
+    MySQLExceptionConverter converter = new MySQLExceptionConverter();
+    Assertions.assertThrows(
+        EntityAlreadyExistsException.class,
+        () -> converter.toGravitinoException(mockException, 
Entity.EntityType.METALAKE, "test"),
+        String.format("The %s entity: %s already exists.", 
Entity.EntityType.METALAKE, "test"));
   }
 }

Reply via email to