From 3dd59a8c59ede2774123fe96eb989adec13c3da3 Mon Sep 17 00:00:00 2001 From: xloya <982052490@qq.com> Date: Mon, 2 Sep 2024 21:45:58 +0800 Subject: [PATCH] [#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 | 39 ++++++++++++++++++ .../TestMySQLExceptionConverter.java | 40 +++++++++++++++++++ 4 files changed, 83 insertions(+), 2 deletions(-) create mode 100644 core/src/test/java/org/apache/gravitino/storage/relational/converters/TestH2ExceptionConverter.java create mode 100644 core/src/test/java/org/apache/gravitino/storage/relational/converters/TestMySQLExceptionConverter.java 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 462fc6a14b8..a64db131a1b 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 void toGravitinoException(SQLException se, Entity.EntityType type, String 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 2d9f34e10ba..1190f9ce4bc 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 void toGravitinoException(SQLException se, Entity.EntityType type, String 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/test/java/org/apache/gravitino/storage/relational/converters/TestH2ExceptionConverter.java b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestH2ExceptionConverter.java new file mode 100644 index 00000000000..f07abd3b593 --- /dev/null +++ b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestH2ExceptionConverter.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.storage.relational.converters; + +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; + +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/test/java/org/apache/gravitino/storage/relational/converters/TestMySQLExceptionConverter.java b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestMySQLExceptionConverter.java new file mode 100644 index 00000000000..9a94db6e106 --- /dev/null +++ b/core/src/test/java/org/apache/gravitino/storage/relational/converters/TestMySQLExceptionConverter.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.storage.relational.converters; + +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; + +public class TestMySQLExceptionConverter { + + @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")); + } +}