Skip to content

Commit

Permalink
[#4735] improvement(core): Optimize the thrown duplicated entry error…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
xloya authored Sep 2, 2024
1 parent 3ba355e commit 3dd59a8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit 3dd59a8

Please sign in to comment.