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")); + } +}