Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Handle NamespaceNotEmptyException in NamespaceErrorHandler #12505

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/iceberg/rest/ErrorHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.iceberg.exceptions.CommitFailedException;
import org.apache.iceberg.exceptions.CommitStateUnknownException;
import org.apache.iceberg.exceptions.ForbiddenException;
import org.apache.iceberg.exceptions.NamespaceNotEmptyException;
import org.apache.iceberg.exceptions.NoSuchNamespaceException;
import org.apache.iceberg.exceptions.NoSuchTableException;
import org.apache.iceberg.exceptions.NoSuchViewException;
Expand Down Expand Up @@ -169,6 +170,11 @@ private static class NamespaceErrorHandler extends DefaultErrorHandler {
@Override
public void accept(ErrorResponse error) {
switch (error.code()) {
case 400:
if (NamespaceNotEmptyException.class.getSimpleName().equals(error.type())) {
throw new NamespaceNotEmptyException("%s", error.message());
}
throw new BadRequestException("Malformed request: %s", error.message());
case 404:
throw new NoSuchNamespaceException("%s", error.message());
case 409:
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/org/apache/iceberg/catalog/CatalogTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
import org.apache.iceberg.UpdateSchema;
import org.apache.iceberg.exceptions.AlreadyExistsException;
import org.apache.iceberg.exceptions.CommitFailedException;
import org.apache.iceberg.exceptions.NamespaceNotEmptyException;
import org.apache.iceberg.exceptions.NoSuchNamespaceException;
import org.apache.iceberg.exceptions.NoSuchTableException;
import org.apache.iceberg.expressions.Expressions;
Expand Down Expand Up @@ -390,6 +391,30 @@ public void testDropNonexistentNamespace() {
.isFalse();
}

@Test
public void testDropNonEmptyNamespace() {
C catalog = catalog();

assertThat(catalog.namespaceExists(NS)).as("Namespace should not exist").isFalse();

catalog.createNamespace(NS);
assertThat(catalog.namespaceExists(NS)).as("Namespace should exist").isTrue();
catalog.buildTable(TABLE, SCHEMA).create();
assertThat(catalog.tableExists(TABLE)).as("Table should exist").isTrue();

assertThatThrownBy(() -> catalog.dropNamespace(NS))
.isInstanceOf(NamespaceNotEmptyException.class)
.hasMessageContaining("is not empty");

catalog.dropTable(TABLE);
assertThat(catalog.tableExists(TABLE)).as("Table should not exist").isFalse();

assertThat(catalog.dropNamespace(NS))
.as("Dropping an existing namespace should return true")
.isTrue();
assertThat(catalog.namespaceExists(NS)).as("Namespace should not exist").isFalse();
}

@Test
public void testListNamespaces() {
C catalog = catalog();
Expand Down