Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#13234] fix(api): put NoSuchEntityException into the NotFoundException hierarchy (#13235)

YangJiedbb9ea319302
diff --git a/api/src/main/java/org/apache/gravitino/exceptions/NoSuchEntityException.java b/api/src/main/java/org/apache/gravitino/exceptions/NoSuchEntityException.java
index 01fe27a950..8fdbecf3e6 100644
--- a/api/src/main/java/org/apache/gravitino/exceptions/NoSuchEntityException.java
+++ b/api/src/main/java/org/apache/gravitino/exceptions/NoSuchEntityException.java
@@ -21,8 +21,12 @@ package org.apache.gravitino.exceptions;
import com.google.errorprone.annotations.FormatMethod;
import com.google.errorprone.annotations.FormatString;
-/** This exception is thrown when an entity is not found. */
-public class NoSuchEntityException extends RuntimeException {
+/**
+ * This exception is thrown when an entity is not found. It belongs to the {@link NotFoundException}
+ * hierarchy so callers catching Gravitino runtime or not-found exceptions see it, like every other
+ * NoSuch* exception.
+ */
+public class NoSuchEntityException extends NotFoundException {
/** The no such entity message for the exception. */
public static final String NO_SUCH_ENTITY_MESSAGE = "No such %s entity: %s";
@@ -34,7 +38,7 @@ public class NoSuchEntityException extends RuntimeException {
*/
@FormatMethod
public NoSuchEntityException(@FormatString String message, Object... args) {
- super(String.format(message, args));
+ super(message, args);
}
/**
@@ -46,6 +50,6 @@ public class NoSuchEntityException extends RuntimeException {
*/
@FormatMethod
public NoSuchEntityException(Throwable cause, @FormatString String message, Object... args) {
- super(String.format(message, args), cause);
+ super(cause, message, args);
}
}
diff --git a/api/src/test/java/org/apache/gravitino/exceptions/TestExceptions.java b/api/src/test/java/org/apache/gravitino/exceptions/TestExceptions.java
new file mode 100644
index 0000000000..7ca970d067
--- /dev/null
+++ b/api/src/test/java/org/apache/gravitino/exceptions/TestExceptions.java
@@ -0,0 +1,46 @@
+/*
+ * 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.exceptions;
+
+import static org.apache.gravitino.exceptions.NoSuchEntityException.NO_SUCH_ENTITY_MESSAGE;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestExceptions {
+
+ @Test
+ public void testNoSuchEntityExceptionIsInGravitinoHierarchy() {
+ // Before the fix, NoSuchEntityException extended RuntimeException directly, outside the
+ // NotFoundException hierarchy every sibling NoSuch* exception uses, so a raw escape mapped to
+ // HTTP 500 instead of 404. Locals are typed Object so the instanceof checks below are evaluated
+ // at runtime against the actual hierarchy (error-prone's BadInstanceof rejects a tautological
+ // check on a statically-known subtype).
+ Object noCause = new NoSuchEntityException(NO_SUCH_ENTITY_MESSAGE, "table", "a.b.c");
+ Object withCause =
+ new NoSuchEntityException(
+ new IllegalStateException("cause"), NO_SUCH_ENTITY_MESSAGE, "table", "a.b.c");
+ // NotFoundException is the parent that drives the 404 mapping; pin it directly rather than only
+ // the broader GravitinoRuntimeException.
+ Assertions.assertTrue(noCause instanceof NotFoundException);
+ Assertions.assertTrue(withCause instanceof NotFoundException);
+ Assertions.assertTrue(noCause instanceof GravitinoRuntimeException);
+ Assertions.assertTrue(withCause instanceof GravitinoRuntimeException);
+ }
+}