Skip to content

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

[#207] feat(server): add exception handlers to unify all the exceptions in rest interface (#245)

Jerry Shaobc17519dc76d
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java
index 0d77202b69..c29506e8b9 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java
@@ -15,11 +15,6 @@ import com.datastrato.graviton.dto.requests.CatalogUpdatesRequest;
import com.datastrato.graviton.dto.responses.CatalogResponse;
import com.datastrato.graviton.dto.responses.DropResponse;
import com.datastrato.graviton.dto.responses.EntityListResponse;
-import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;
-import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
-import com.datastrato.graviton.exceptions.IllegalNamespaceException;
-import com.datastrato.graviton.exceptions.NoSuchCatalogException;
-import com.datastrato.graviton.exceptions.NoSuchMetalakeException;
import com.datastrato.graviton.server.web.Utils;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
@@ -61,17 +56,8 @@ public class CatalogOperations {
NameIdentifier[] idents = manager.listCatalogs(catalogNS);
return Utils.ok(new EntityListResponse(idents));
- } catch (IllegalNamespaceException e) {
- LOG.warn("Failed to list catalogs with invalid namespace", e);
- return Utils.illegalArguments("Failed to list catalogs with invalid namespace", e);
-
- } catch (NoSuchMetalakeException ex) {
- LOG.error("Metalake {} does not exist, fail to list catalogs", metalake);
- return Utils.notFound("Metalake " + metalake + " does not exist", ex);
-
} catch (Exception e) {
- LOG.error("Failed to list catalogs under metalake {}", metalake, e);
- return Utils.internalError("Failed to list catalogs under metalake " + metalake, e);
+ return ExceptionHandlers.handleCatalogException(OperationType.LIST, "", metalake, e);
}
}
@@ -81,36 +67,15 @@ public class CatalogOperations {
@PathParam("metalake") String metalake, CatalogCreateRequest request) {
try {
request.validate();
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate CreateCatalogRequest arguments {}", request, e);
- return Utils.illegalArguments(
- "Failed to validate CreateCatalogRequest arguments " + request, e);
- }
-
- try {
NameIdentifier ident = NameIdentifier.ofCatalog(metalake, request.getName());
Catalog catalog =
manager.createCatalog(
ident, request.getType(), request.getComment(), request.getProperties());
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to create catalog with invalid name identifier", e);
- return Utils.illegalArguments("Failed to create catalog with invalid name identifier", e);
-
- } catch (NoSuchMetalakeException ex) {
- LOG.error("Metalake {} does not exist, fail to create catalog", metalake);
- return Utils.notFound("Metalake " + metalake + " does not exist", ex);
-
- } catch (CatalogAlreadyExistsException ex) {
- LOG.error("Catalog {} already exists under metalake {}", request.getName(), metalake);
- return Utils.alreadyExists(
- String.format("Catalog %s already exists under metalake %s", request.getName(), metalake),
- ex);
-
} catch (Exception e) {
- LOG.error("Failed to create catalog under metalake {}", metalake, e);
- return Utils.internalError("Failed to create catalog under metalake " + metalake, e);
+ return ExceptionHandlers.handleCatalogException(
+ OperationType.CREATE, request.getName(), metalake, e);
}
}
@@ -124,24 +89,9 @@ public class CatalogOperations {
Catalog catalog = manager.loadCatalog(ident);
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to load catalog with invalid name identifier", e);
- return Utils.illegalArguments("Failed to load catalog with invalid name identifier", e);
-
- } catch (NoSuchMetalakeException ex) {
- LOG.error("Metalake {} does not exist, fail to load catalog {}", metalakeName, catalogName);
- return Utils.notFound("Metalake " + metalakeName + " does not exist", ex);
-
- } catch (NoSuchCatalogException ex) {
- LOG.error("Catalog {} does not exist under metalake {}", catalogName, metalakeName);
- return Utils.notFound(
- String.format("Catalog %s does not exist under metalake %s", catalogName, metalakeName),
- ex);
-
} catch (Exception e) {
- LOG.error("Failed to load catalog {} under metalake {}", catalogName, metalakeName, e);
- return Utils.internalError(
- "Failed to load catalog " + catalogName + " under metalake " + metalakeName, e);
+ return ExceptionHandlers.handleCatalogException(
+ OperationType.LOAD, catalogName, metalakeName, e);
}
}
@@ -154,13 +104,6 @@ public class CatalogOperations {
CatalogUpdatesRequest request) {
try {
request.validate();
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate CatalogUpdatesRequest arguments {}", request, e);
- return Utils.illegalArguments(
- "Failed to validate CatalogUpdatesRequest arguments " + request, e);
- }
-
- try {
NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
CatalogChange[] changes =
request.getUpdates().stream()
@@ -170,33 +113,9 @@ public class CatalogOperations {
Catalog catalog = manager.alterCatalog(ident, changes);
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to alter catalog with invalid name identifier", e);
- return Utils.illegalArguments("Failed to alter catalog with invalid name identifier", e);
-
- } catch (NoSuchCatalogException ex) {
- LOG.error("Catalog {} does not exist under metalake {}", catalogName, metalakeName);
- return Utils.notFound(
- "Catalog " + catalogName + " does not exist under metalake " + metalakeName, ex);
-
- } catch (IllegalArgumentException ex) {
- LOG.error(
- "Failed to alter catalog {} under metalake {} with unsupported changes",
- catalogName,
- metalakeName,
- ex);
- return Utils.illegalArguments(
- "Failed to alter catalog "
- + catalogName
- + " under metalake "
- + metalakeName
- + " with unsupported changes",
- ex);
-
} catch (Exception e) {
- LOG.error("Failed to alter catalog {} under metalake {}", catalogName, metalakeName, e);
- return Utils.internalError(
- "Failed to alter catalog " + catalogName + " under metalake " + metalakeName, e);
+ return ExceptionHandlers.handleCatalogException(
+ OperationType.ALTER, catalogName, metalakeName, e);
}
}
@@ -214,14 +133,9 @@ public class CatalogOperations {
return Utils.ok(new DropResponse(dropped));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to drop catalog with invalid name identifier", e);
- return Utils.illegalArguments("Failed to drop catalog with invalid name identifier", e);
-
} catch (Exception e) {
- LOG.error("Failed to drop catalog {} under metalake {}", catalogName, metalakeName, e);
- return Utils.internalError(
- "Failed to drop catalog " + catalogName + " under metalake " + metalakeName, e);
+ return ExceptionHandlers.handleCatalogException(
+ OperationType.DROP, catalogName, metalakeName, e);
}
}
}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/ExceptionHandler.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/ExceptionHandler.java
new file mode 100644
index 0000000000..fbf6880390
--- /dev/null
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/ExceptionHandler.java
@@ -0,0 +1,28 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.server.web.rest;
+
+import javax.ws.rs.core.Response;
+
+/**
+ * The ExceptionHandler class is an abstract class specialized for handling Exceptions and returning
+ * the appropriate Response.Subclasses of ExceptionHandler must implement {@link
+ * ExceptionHandler#handle(OperationType, String, String, Exception)} to provide the {@link
+ * Response} related to the exception.
+ */
+public abstract class ExceptionHandler {
+
+ /**
+ * Handles the exception and returns the appropriate Response. The implementation will use the
+ * provided parameters to form a standard error response.
+ *
+ * @param op The operation that was attempted
+ * @param object The object name that was being operated on
+ * @param parent The parent object name that was being operated on
+ * @param e The exception that was thrown
+ * @return The Response object representing the error response
+ */
+ public abstract Response handle(OperationType op, String object, String parent, Exception e);
+}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/ExceptionHandlers.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/ExceptionHandlers.java
new file mode 100644
index 0000000000..927a51e699
--- /dev/null
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/ExceptionHandlers.java
@@ -0,0 +1,190 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.server.web.rest;
+
+import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;
+import com.datastrato.graviton.exceptions.MetalakeAlreadyExistsException;
+import com.datastrato.graviton.exceptions.NoSuchMetalakeException;
+import com.datastrato.graviton.exceptions.NonEmptySchemaException;
+import com.datastrato.graviton.exceptions.NotFoundException;
+import com.datastrato.graviton.exceptions.SchemaAlreadyExistsException;
+import com.datastrato.graviton.exceptions.TableAlreadyExistsException;
+import com.datastrato.graviton.server.web.Utils;
+import javax.ws.rs.core.Response;
+import org.eclipse.jetty.util.StringUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ExceptionHandlers {
+
+ private static final Logger LOG = LoggerFactory.getLogger(ExceptionHandlers.class);
+
+ private ExceptionHandlers() {}
+
+ public static Response handleTableException(
+ OperationType op, String table, String schema, Exception e) {
+ return TableExceptionHandler.INSTANCE.handle(op, table, schema, e);
+ }
+
+ public static Response handleSchemaException(
+ OperationType op, String schema, String catalog, Exception e) {
+ return SchemaExceptionHandler.INSTANCE.handle(op, schema, catalog, e);
+ }
+
+ public static Response handleCatalogException(
+ OperationType op, String catalog, String metalake, Exception e) {
+ return CatalogExceptionHandler.INSTANCE.handle(op, catalog, metalake, e);
+ }
+
+ public static Response handleMetalakeException(OperationType op, String metalake, Exception e) {
+ return MetalakeExceptionHandler.INSTANCE.handle(op, metalake, "", e);
+ }
+
+ private static class TableExceptionHandler extends BaseExceptionHandler {
+
+ private static final ExceptionHandler INSTANCE = new TableExceptionHandler();
+
+ private static final String TABLE_MSG_TEMPLATE =
+ "Failed to operate table(s)%s operation [%s] under schema [%s], reason [%s]";
+
+ @Override
+ public Response handle(OperationType op, String table, String schema, Exception e) {
+ String formatted = StringUtil.isBlank(table) ? "" : " [" + table + "]";
+ String errorMsg =
+ String.format(
+ TABLE_MSG_TEMPLATE, formatted, op.name(), schema, e.getClass().getSimpleName());
+ LOG.warn(errorMsg, e);
+
+ if (e instanceof IllegalArgumentException) {
+ return Utils.illegalArguments(errorMsg, e);
+
+ } else if (e instanceof NotFoundException) {
+ return Utils.notFound(errorMsg, e);
+
+ } else if (e instanceof TableAlreadyExistsException) {
+ return Utils.alreadyExists(errorMsg, e);
+
+ } else {
+ return super.handle(op, table, schema, e);
+ }
+ }
+ }
+
+ private static class SchemaExceptionHandler extends BaseExceptionHandler {
+
+ private static final ExceptionHandler INSTANCE = new SchemaExceptionHandler();
+
+ private static final String SCHEMA_MSG_TEMPLATE =
+ "Failed to operate schema(s)%s operation [%s] under catalog [%s], reason [%s]";
+
+ @Override
+ public Response handle(OperationType op, String schema, String catalog, Exception e) {
+ String formatted = StringUtil.isBlank(schema) ? "" : " [" + schema + "]";
+ String errorMsg =
+ String.format(
+ SCHEMA_MSG_TEMPLATE, formatted, op.name(), catalog, e.getClass().getSimpleName());
+ LOG.warn(errorMsg, e);
+
+ if (e instanceof IllegalArgumentException) {
+ return Utils.illegalArguments(errorMsg, e);
+
+ } else if (e instanceof NotFoundException) {
+ return Utils.notFound(errorMsg, e);
+
+ } else if (e instanceof SchemaAlreadyExistsException) {
+ return Utils.alreadyExists(errorMsg, e);
+
+ } else if (e instanceof NonEmptySchemaException) {
+ return Utils.nonEmpty(errorMsg, e);
+
+ } else {
+ return super.handle(op, schema, catalog, e);
+ }
+ }
+ }
+
+ private static class CatalogExceptionHandler extends BaseExceptionHandler {
+
+ private static final ExceptionHandler INSTANCE = new CatalogExceptionHandler();
+
+ private static final String CATALOG_MSG_TEMPLATE =
+ "Failed to operate catalog(s)%s operation [%s] under metalake [%s], reason [%s]";
+
+ @Override
+ public Response handle(OperationType op, String catalog, String metalake, Exception e) {
+ String formatted = StringUtil.isBlank(catalog) ? "" : " [" + catalog + "]";
+ String errorMsg =
+ String.format(
+ CATALOG_MSG_TEMPLATE, formatted, op.name(), metalake, e.getClass().getSimpleName());
+ LOG.warn(errorMsg, e);
+
+ if (e instanceof IllegalArgumentException) {
+ return Utils.illegalArguments(errorMsg, e);
+
+ } else if (e instanceof NotFoundException) {
+ return Utils.notFound(errorMsg, e);
+
+ } else if (e instanceof CatalogAlreadyExistsException) {
+ return Utils.alreadyExists(errorMsg, e);
+
+ } else {
+ return super.handle(op, catalog, metalake, e);
+ }
+ }
+ }
+
+ private static class MetalakeExceptionHandler extends BaseExceptionHandler {
+
+ private static final ExceptionHandler INSTANCE = new MetalakeExceptionHandler();
+
+ private static final String METALAKE_MSG_TEMPLATE =
+ "Failed to operate metalake(s)%s operation [%s], reason [%s]";
+
+ @Override
+ public Response handle(OperationType op, String metalake, String parent, Exception e) {
+ String formatted = StringUtil.isBlank(metalake) ? "" : " [" + metalake + "]";
+ String errorMsg =
+ String.format(METALAKE_MSG_TEMPLATE, formatted, op.name(), e.getClass().getSimpleName());
+ LOG.warn(errorMsg, e);
+
+ if (e instanceof IllegalArgumentException) {
+ return Utils.illegalArguments(errorMsg, e);
+
+ } else if (e instanceof MetalakeAlreadyExistsException) {
+ return Utils.alreadyExists(errorMsg, e);
+
+ } else if (e instanceof NoSuchMetalakeException) {
+ return Utils.notFound(errorMsg, e);
+
+ } else {
+ return super.handle(op, metalake, parent, e);
+ }
+ }
+ }
+
+ private static class BaseExceptionHandler extends ExceptionHandler {
+
+ private static final ExceptionHandler INSTANCE = new BaseExceptionHandler();
+
+ private static final String BASE_MSG_TEMPLATE =
+ "Failed to operate object%s operation [%s]%s, reason [%s]";
+
+ @Override
+ public Response handle(OperationType op, String object, String parent, Exception e) {
+ String formattedObject = StringUtil.isBlank(object) ? "" : " [" + object + "]";
+ String formattedParent = StringUtil.isBlank(parent) ? "" : " under [" + parent + "]";
+
+ String errorMsg =
+ String.format(
+ BASE_MSG_TEMPLATE,
+ formattedObject,
+ op.name(),
+ formattedParent,
+ e.getClass().getSimpleName());
+ LOG.error(errorMsg, e);
+ return Utils.internalError(errorMsg, e);
+ }
+ }
+}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
index 15d45d12e2..dbbbb63a63 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
@@ -6,6 +6,7 @@ package com.datastrato.graviton.server.web.rest;
import com.datastrato.graviton.MetalakeChange;
import com.datastrato.graviton.NameIdentifier;
+import com.datastrato.graviton.Namespace;
import com.datastrato.graviton.dto.MetalakeDTO;
import com.datastrato.graviton.dto.requests.MetalakeCreateRequest;
import com.datastrato.graviton.dto.requests.MetalakeUpdateRequest;
@@ -13,10 +14,6 @@ import com.datastrato.graviton.dto.requests.MetalakeUpdatesRequest;
import com.datastrato.graviton.dto.responses.DropResponse;
import com.datastrato.graviton.dto.responses.MetalakeListResponse;
import com.datastrato.graviton.dto.responses.MetalakeResponse;
-import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
-import com.datastrato.graviton.exceptions.IllegalNamespaceException;
-import com.datastrato.graviton.exceptions.MetalakeAlreadyExistsException;
-import com.datastrato.graviton.exceptions.NoSuchMetalakeException;
import com.datastrato.graviton.meta.BaseMetalake;
import com.datastrato.graviton.meta.MetalakeManager;
import com.datastrato.graviton.server.web.Utils;
@@ -63,8 +60,8 @@ public class MetalakeOperations {
return Utils.ok(new MetalakeListResponse(metalakeDTOS));
} catch (Exception e) {
- LOG.error("Failed to list metalakes", e);
- return Utils.internalError("Failed to list metalake", e);
+ return ExceptionHandlers.handleMetalakeException(
+ OperationType.LIST, Namespace.empty().toString(), e);
}
}
@@ -73,30 +70,13 @@ public class MetalakeOperations {
public Response createMetalake(MetalakeCreateRequest request) {
try {
request.validate();
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate MetalakeCreateRequest arguments {}", request, e);
-
- return Utils.illegalArguments(
- "Failed to validate MetalakeCreateRequest arguments " + request, e);
- }
-
- try {
NameIdentifier ident = NameIdentifier.ofMetalake(request.getName());
BaseMetalake metalake =
manager.createMetalake(ident, request.getComment(), request.getProperties());
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to create metalake with invalid arguments", e);
- return Utils.illegalArguments("Failed to create metalake with invalid arguments", e);
-
- } catch (MetalakeAlreadyExistsException exception) {
- LOG.warn("Metalake {} already exists", request.getName(), exception);
- return Utils.alreadyExists("Metalake " + request.getName() + " already exists", exception);
-
} catch (Exception e) {
- LOG.error("Failed to create metalake", e);
- return Utils.internalError("Failed to create metalake", e);
+ return ExceptionHandlers.handleMetalakeException(OperationType.CREATE, request.getName(), e);
}
}
@@ -109,17 +89,8 @@ public class MetalakeOperations {
BaseMetalake metalake = manager.loadMetalake(identifier);
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to load metalake with invalid arguments", e);
- return Utils.illegalArguments("Failed to load metalake with invalid arguments", e);
-
- } catch (NoSuchMetalakeException e) {
- LOG.warn("Metalake {} does not exist", metalakeName);
- return Utils.notFound("Metalake " + metalakeName + " does not exist", e);
-
} catch (Exception e) {
- LOG.error("Failed to load metalake {}", metalakeName, e);
- return Utils.internalError("Failed to load metalake " + metalakeName, e);
+ return ExceptionHandlers.handleMetalakeException(OperationType.LOAD, metalakeName, e);
}
}
@@ -130,13 +101,6 @@ public class MetalakeOperations {
@PathParam("name") String metalakeName, MetalakeUpdatesRequest updatesRequest) {
try {
updatesRequest.validate();
- } catch (Exception e) {
- LOG.error("Failed to validate MetalakeUpdatesRequest arguments {}", updatesRequest, e);
- return Utils.illegalArguments(
- "Failed to validate MetalakeUpdatesRequest arguments " + updatesRequest, e);
- }
-
- try {
NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
MetalakeChange[] changes =
updatesRequest.getUpdates().stream()
@@ -146,22 +110,8 @@ public class MetalakeOperations {
BaseMetalake updatedMetalake = manager.alterMetalake(identifier, changes);
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(updatedMetalake)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to alter metalake with invalid arguments", e);
- return Utils.illegalArguments("Failed to alter metalake with invalid arguments", e);
-
- } catch (NoSuchMetalakeException e) {
- LOG.warn("Metalake {} does not exist", metalakeName);
- return Utils.notFound("Metalake " + metalakeName + " does not exist", e);
-
- } catch (IllegalArgumentException ex) {
- LOG.error("Failed to alter metalake {} by unsupported change", metalakeName, ex);
- return Utils.illegalArguments(
- "Failed to alter metalake " + metalakeName + " by unsupported change", ex);
-
} catch (Exception e) {
- LOG.error("Failed to update metalake {}", metalakeName, e);
- return Utils.internalError("Failed to update metalake " + metalakeName, e);
+ return ExceptionHandlers.handleMetalakeException(OperationType.ALTER, metalakeName, e);
}
}
@@ -178,13 +128,8 @@ public class MetalakeOperations {
return Utils.ok(new DropResponse(dropped));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to drop metalake with invalid arguments", e);
- return Utils.illegalArguments("Failed to drop metalake with invalid arguments", e);
-
} catch (Exception e) {
- LOG.error("Failed to drop metalake {}", metalakeName, e);
- return Utils.internalError("Failed to drop metalake " + metalakeName, e);
+ return ExceptionHandlers.handleMetalakeException(OperationType.DROP, metalakeName, e);
}
}
}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/OperationType.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/OperationType.java
new file mode 100644
index 0000000000..146f73e858
--- /dev/null
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/OperationType.java
@@ -0,0 +1,13 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.server.web.rest;
+
+public enum OperationType {
+ LIST,
+ CREATE,
+ LOAD,
+ ALTER,
+ DROP;
+}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/SchemaOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/SchemaOperations.java
index 32144411a4..952da5516c 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/SchemaOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/SchemaOperations.java
@@ -13,12 +13,6 @@ import com.datastrato.graviton.dto.requests.SchemaUpdatesRequest;
import com.datastrato.graviton.dto.responses.DropResponse;
import com.datastrato.graviton.dto.responses.EntityListResponse;
import com.datastrato.graviton.dto.responses.SchemaResponse;
-import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
-import com.datastrato.graviton.exceptions.IllegalNamespaceException;
-import com.datastrato.graviton.exceptions.NoSuchCatalogException;
-import com.datastrato.graviton.exceptions.NoSuchSchemaException;
-import com.datastrato.graviton.exceptions.NonEmptySchemaException;
-import com.datastrato.graviton.exceptions.SchemaAlreadyExistsException;
import com.datastrato.graviton.rel.Schema;
import com.datastrato.graviton.rel.SchemaChange;
import com.datastrato.graviton.server.web.Utils;
@@ -65,17 +59,8 @@ public class SchemaOperations {
NameIdentifier[] idents = dispatcher.listSchemas(schemaNS);
return Utils.ok(new EntityListResponse(idents));
- } catch (IllegalNamespaceException e) {
- LOG.error("Failed to list schemas with invalid namespace", e);
- return Utils.illegalArguments("Failed to list schemas with invalid namespace", e);
-
- } catch (NoSuchCatalogException e) {
- LOG.error("Catalog {} does not exist, fail to list schemas", catalog);
- return Utils.notFound("catalog " + catalog + " does not exist", e);
-
} catch (Exception e) {
- LOG.error("Fail to list schemas under catalog {}", catalog, e);
- return Utils.internalError("Fail to list schemas under catalog " + catalog, e);
+ return ExceptionHandlers.handleSchemaException(OperationType.LIST, "", catalog, e);
}
}
@@ -87,36 +72,13 @@ public class SchemaOperations {
SchemaCreateRequest request) {
try {
request.validate();
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate CreateSchemaRequest arguments {}", request, e);
- return Utils.illegalArguments(
- "Failed to validate CreateSchemaRequest arguments " + request, e);
- }
-
- try {
NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, request.getName());
Schema schema = dispatcher.createSchema(ident, request.getComment(), request.getProperties());
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(schema)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to create schema with invalid namespace", e);
- return Utils.illegalArguments("Failed to create schema with invalid namespace", e);
-
- } catch (NoSuchCatalogException e) {
- LOG.error(
- "Catalog {} does not exist, fail to create schema {}", catalog, request.getName(), e);
- return Utils.notFound(
- "Catalog " + catalog + " does not exist, fail to create schema " + request.getName(), e);
-
- } catch (SchemaAlreadyExistsException e) {
- LOG.error("Schema {} already exists under {}", request.getComment(), catalog, e);
- return Utils.alreadyExists(
- "Schema " + request.getName() + " already exists under catalog " + catalog, e);
-
} catch (Exception e) {
- LOG.error("Fail to create schema {} under catalog {}", request.getName(), catalog, e);
- return Utils.internalError(
- "Fail to create schema " + request.getName() + " under catalog " + catalog, e);
+ return ExceptionHandlers.handleSchemaException(
+ OperationType.CREATE, request.getName(), catalog, e);
}
}
@@ -132,17 +94,8 @@ public class SchemaOperations {
Schema s = dispatcher.loadSchema(ident);
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to load schema with invalid namespace", e);
- return Utils.illegalArguments("Failed to load schema with invalid namespace", e);
-
- } catch (NoSuchSchemaException e) {
- LOG.error("Schema {} does not exist under catalog {}", schema, catalog);
- return Utils.notFound("Schema " + schema + " does not exist under catalog " + catalog, e);
-
} catch (Exception e) {
- LOG.error("Fail to load schema {} under catalog {}", schema, catalog, e);
- return Utils.internalError("Fail to load schema " + schema + " under catalog " + catalog, e);
+ return ExceptionHandlers.handleSchemaException(OperationType.LOAD, schema, catalog, e);
}
}
@@ -156,32 +109,16 @@ public class SchemaOperations {
SchemaUpdatesRequest request) {
try {
request.validate();
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate SchemaUpdatesRequest arguments {}", request, e);
- return Utils.illegalArguments(
- "Failed to validate SchemaUpdatesRequest arguments " + request, e);
- }
-
- SchemaChange[] changes =
- request.getUpdates().stream()
- .map(SchemaUpdateRequest::schemaChange)
- .toArray(SchemaChange[]::new);
- try {
NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
+ SchemaChange[] changes =
+ request.getUpdates().stream()
+ .map(SchemaUpdateRequest::schemaChange)
+ .toArray(SchemaChange[]::new);
Schema s = dispatcher.alterSchema(ident, changes);
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to alter schema with invalid namespace", e);
- return Utils.illegalArguments("Failed to alter schema with invalid namespace", e);
-
- } catch (NoSuchSchemaException e) {
- LOG.error("Schema {} does not exist under catalog {}", schema, catalog);
- return Utils.notFound("Schema " + schema + " does not exist under catalog " + catalog, e);
-
} catch (Exception e) {
- LOG.error("Fail to alter schema {} under catalog {}", schema, catalog, e);
- return Utils.internalError("Fail to alter schema " + schema + " under catalog " + catalog, e);
+ return ExceptionHandlers.handleSchemaException(OperationType.ALTER, schema, catalog, e);
}
}
@@ -202,25 +139,8 @@ public class SchemaOperations {
return Utils.ok(new DropResponse(dropped));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to drop schema with invalid namespace", e);
- return Utils.illegalArguments("Failed to drop schema with invalid namespace", e);
-
- } catch (NonEmptySchemaException e) {
- LOG.error(
- "Schema {} is not empty under catalog {}, but cascade is set to true", schema, catalog);
- return Utils.nonEmpty(
- "Schema "
- + schema
- + " is not empty under catalog "
- + catalog
- + " , but "
- + "cascade is set to true",
- e);
-
} catch (Exception e) {
- LOG.error("Fail to drop schema {} under catalog {}", schema, catalog, e);
- return Utils.internalError("Fail to drop schema " + schema + " under catalog " + catalog, e);
+ return ExceptionHandlers.handleSchemaException(OperationType.DROP, schema, catalog, e);
}
}
}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java
index c1a276bd13..dcdc271d18 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java
@@ -13,11 +13,6 @@ import com.datastrato.graviton.dto.requests.TableUpdatesRequest;
import com.datastrato.graviton.dto.responses.DropResponse;
import com.datastrato.graviton.dto.responses.EntityListResponse;
import com.datastrato.graviton.dto.responses.TableResponse;
-import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
-import com.datastrato.graviton.exceptions.IllegalNamespaceException;
-import com.datastrato.graviton.exceptions.NoSuchSchemaException;
-import com.datastrato.graviton.exceptions.NoSuchTableException;
-import com.datastrato.graviton.exceptions.TableAlreadyExistsException;
import com.datastrato.graviton.rel.Table;
import com.datastrato.graviton.rel.TableChange;
import com.datastrato.graviton.server.web.Utils;
@@ -60,17 +55,8 @@ public class TableOperations {
NameIdentifier[] idents = dispatcher.listTables(tableNS);
return Utils.ok(new EntityListResponse(idents));
- } catch (IllegalNamespaceException e) {
- LOG.error("Failed to list tables with invalid arguments", e);
- return Utils.illegalArguments("Failed to list tables with invalid arguments", e);
-
- } catch (NoSuchSchemaException e) {
- LOG.error("Schema {} does not exist, fail to list tables", schema);
- return Utils.notFound("Schema " + schema + " does not exist, fail to list tables", e);
-
} catch (Exception e) {
- LOG.error("Failed to list tables under schema {}", schema, e);
- return Utils.internalError("Failed to list tables under schema " + schema, e);
+ return ExceptionHandlers.handleTableException(OperationType.LIST, "", schema, e);
}
}
@@ -83,37 +69,15 @@ public class TableOperations {
TableCreateRequest request) {
try {
request.validate();
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate TableCreateRequest arguments {}", request, e);
- return Utils.illegalArguments("Failed to validate TableCreateRequest arguments", e);
- }
-
- try {
NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, request.getName());
Table table =
dispatcher.createTable(
ident, request.getColumns(), request.getComment(), request.getProperties());
return Utils.ok(new TableResponse(DTOConverters.toDTO(table)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to create table {} with invalid arguments", request.getName(), e);
- return Utils.illegalArguments(
- "Failed to create table " + request.getName() + " with invalid arguments", e);
-
- } catch (NoSuchSchemaException e) {
- LOG.error("Schema {} does not exist, fail to create table {}", schema, request.getName());
- return Utils.notFound(
- "Schema " + schema + " does not exist, fail to create table " + request.getName(), e);
-
- } catch (TableAlreadyExistsException e) {
- LOG.error("Table {} already exists under schema {}", request.getName(), schema);
- return Utils.alreadyExists(
- "Table " + request.getName() + " already exists under schema " + schema, e);
-
} catch (Exception e) {
- LOG.error("Failed to create table {} under schema {}", request.getName(), schema, e);
- return Utils.internalError(
- "Failed to create table " + request.getName() + " under schema " + schema, e);
+ return ExceptionHandlers.handleTableException(
+ OperationType.CREATE, request.getName(), schema, e);
}
}
@@ -130,17 +94,8 @@ public class TableOperations {
Table t = dispatcher.loadTable(ident);
return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to load table {} with invalid arguments", table, e);
- return Utils.illegalArguments("Failed to load table " + table + " with invalid arguments", e);
-
- } catch (NoSuchTableException e) {
- LOG.error("Table {} does not exist under schema {}", table, schema);
- return Utils.notFound("Table " + table + " does not exist under schema " + schema, e);
-
} catch (Exception e) {
- LOG.error("Failed to load table {} under schema {}", table, schema, e);
- return Utils.internalError("Failed to load table " + table + " under schema " + schema, e);
+ return ExceptionHandlers.handleTableException(OperationType.LOAD, table, schema, e);
}
}
@@ -155,12 +110,6 @@ public class TableOperations {
TableUpdatesRequest request) {
try {
request.validate();
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate Request arguments {}", request, e);
- return Utils.illegalArguments("Failed to validate TableAlterRequest arguments", e);
- }
-
- try {
NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
TableChange[] changes =
request.getUpdates().stream()
@@ -169,18 +118,8 @@ public class TableOperations {
Table t = dispatcher.alterTable(ident, changes);
return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
- } catch (IllegalNameIdentifierException | IllegalNamespaceException e) {
- LOG.warn("Failed to alter table {} with invalid arguments", table, e);
- return Utils.illegalArguments(
- "Failed to alter table " + table + " with invalid arguments", e);
-
- } catch (NoSuchTableException e) {
- LOG.error("Table {} does not exist under schema {}", table, schema);
- return Utils.notFound("Table " + table + " does not exist under schema " + schema, e);
-
} catch (Exception e) {
- LOG.error("Failed to alter table {} under schema {}", table, schema, e);
- return Utils.internalError("Failed to alter table " + table + " under schema " + schema, e);
+ return ExceptionHandlers.handleTableException(OperationType.ALTER, table, schema, e);
}
}
@@ -201,13 +140,8 @@ public class TableOperations {
return Utils.ok(new DropResponse(dropped));
- } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
- LOG.warn("Failed to drop table {} with invalid arguments", table, e);
- return Utils.illegalArguments("Failed to drop table " + table + " with invalid arguments", e);
-
} catch (Exception e) {
- LOG.error("Failed to drop table {} under schema {}", table, schema, e);
- return Utils.internalError("Failed to drop table " + table + " under schema " + schema, e);
+ return ExceptionHandlers.handleTableException(OperationType.DROP, table, schema, e);
}
}
}
diff --git a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakeOperations.java b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakeOperations.java
index 47dce7df44..3ccfcd7dc8 100644
--- a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakeOperations.java
+++ b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakeOperations.java
@@ -201,8 +201,10 @@ public class TestMetalakeOperations extends JerseyTest {
ErrorResponse errorResponse = resp1.readEntity(ErrorResponse.class);
Assertions.assertEquals(ErrorConstants.NOT_FOUND_CODE, errorResponse.getCode());
Assertions.assertEquals(NoSuchMetalakeException.class.getSimpleName(), errorResponse.getType());
- Assertions.assertEquals(
- "Metalake " + metalakeName + " does not exist", errorResponse.getMessage());
+ Assertions.assertTrue(
+ errorResponse
+ .getMessage()
+ .contains("Failed to operate metalake(s) [" + metalakeName + "] operation [LOAD]"));
// Test with internal error
doThrow(new RuntimeException("Internal error")).when(metalakeManager).loadMetalake(any());
@@ -219,7 +221,10 @@ public class TestMetalakeOperations extends JerseyTest {
ErrorResponse errorResponse1 = resp2.readEntity(ErrorResponse.class);
Assertions.assertEquals(ErrorConstants.INTERNAL_ERROR_CODE, errorResponse1.getCode());
Assertions.assertEquals(RuntimeException.class.getSimpleName(), errorResponse1.getType());
- Assertions.assertEquals("Failed to load metalake " + metalakeName, errorResponse1.getMessage());
+ Assertions.assertTrue(
+ errorResponse1
+ .getMessage()
+ .contains("Failed to operate object [" + metalakeName + "] operation [LOAD]"));
}
@Test
@@ -330,6 +335,7 @@ public class TestMetalakeOperations extends JerseyTest {
ErrorResponse errorResponse = resp1.readEntity(ErrorResponse.class);
Assertions.assertEquals(ErrorConstants.INTERNAL_ERROR_CODE, errorResponse.getCode());
Assertions.assertEquals(RuntimeException.class.getSimpleName(), errorResponse.getType());
- Assertions.assertEquals("Failed to drop metalake test", errorResponse.getMessage());
+ Assertions.assertTrue(
+ errorResponse.getMessage().contains("Failed to operate object [test] operation [DROP]"));
}
}