World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#1551] improve(server): Optimize the error message (#1620)
Jerry Shao0ec0d3dda9fa
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/ExceptionHandlers.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/ExceptionHandlers.javaindex 1a8b9059f2..99b973b7f8 100644--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/ExceptionHandlers.java+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/ExceptionHandlers.java@@ -12,6 +12,7 @@ import com.datastrato.gravitino.exceptions.NotFoundException;import com.datastrato.gravitino.exceptions.SchemaAlreadyExistsException;import com.datastrato.gravitino.exceptions.TableAlreadyExistsException;import com.datastrato.gravitino.server.web.Utils;+import com.google.common.annotations.VisibleForTesting;import javax.ws.rs.core.Response;import org.eclipse.jetty.util.StringUtil;import org.slf4j.Logger;@@ -53,8 +54,7 @@ public class ExceptionHandlers {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());+ String.format(TABLE_MSG_TEMPLATE, formatted, op.name(), schema, getErrorMsg(e));LOG.warn(errorMsg, e);if (e instanceof IllegalArgumentException) {@@ -86,8 +86,7 @@ public class ExceptionHandlers {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());+ String.format(SCHEMA_MSG_TEMPLATE, formatted, op.name(), catalog, getErrorMsg(e));LOG.warn(errorMsg, e);if (e instanceof IllegalArgumentException) {@@ -119,8 +118,7 @@ public class ExceptionHandlers {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());+ String.format(CATALOG_MSG_TEMPLATE, formatted, op.name(), metalake, getErrorMsg(e));LOG.warn(errorMsg, e);if (e instanceof IllegalArgumentException) {@@ -148,8 +146,7 @@ public class ExceptionHandlers {@Overridepublic 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());+ String errorMsg = String.format(METALAKE_MSG_TEMPLATE, formatted, op.name(), getErrorMsg(e));LOG.warn(errorMsg, e);if (e instanceof IllegalArgumentException) {@@ -167,7 +164,10 @@ public class ExceptionHandlers {}}- private static class BaseExceptionHandler extends ExceptionHandler {+ @VisibleForTesting+ static class BaseExceptionHandler extends ExceptionHandler {++ private static final String EXCEPTION_KEYWORD = "Exception: ";private static final ExceptionHandler INSTANCE = new BaseExceptionHandler();@@ -181,13 +181,24 @@ public class ExceptionHandlers {String errorMsg =String.format(- BASE_MSG_TEMPLATE,- formattedObject,- op.name(),- formattedParent,- e.getClass().getSimpleName());+ BASE_MSG_TEMPLATE, formattedObject, op.name(), formattedParent, getErrorMsg(e));LOG.error(errorMsg, e);return Utils.internalError(errorMsg, e);}++ @VisibleForTesting+ static String getErrorMsg(Throwable throwable) {+ if (throwable == null || throwable.getMessage() == null) {+ return "";+ }++ String message = throwable.getMessage();+ int pos = message.lastIndexOf(EXCEPTION_KEYWORD);+ if (pos == -1) {+ return message;+ } else {+ return message.substring(pos + EXCEPTION_KEYWORD.length());+ }+ }}}diff --git a/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestExceptionHandlers.java b/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestExceptionHandlers.javanew file mode 100644index 0000000000..54a057ebbd--- /dev/null+++ b/server/src/test/java/com/datastrato/gravitino/server/web/rest/TestExceptionHandlers.java@@ -0,0 +1,39 @@+/*+ * Copyright 2024 Datastrato Pvt Ltd.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.gravitino.server.web.rest;++import org.junit.jupiter.api.Assertions;+import org.junit.jupiter.api.Test;++public class TestExceptionHandlers {++ @Test+ public void testGetErrorMsg() {+ Exception e1 = new Exception("test1");+ Exception e2 = new Exception("test2", e1);+ Exception e3 = new Exception(e1);+ Exception e4 = new Exception();+ Exception e5 = new Exception(e2);+ Exception e6 = null;++ String msg1 = ExceptionHandlers.BaseExceptionHandler.getErrorMsg(e1);+ Assertions.assertEquals("test1", msg1);++ String msg2 = ExceptionHandlers.BaseExceptionHandler.getErrorMsg(e2);+ Assertions.assertEquals("test2", msg2);++ String msg3 = ExceptionHandlers.BaseExceptionHandler.getErrorMsg(e3);+ Assertions.assertEquals("test1", msg3);++ String msg4 = ExceptionHandlers.BaseExceptionHandler.getErrorMsg(e4);+ Assertions.assertEquals("", msg4);++ String msg5 = ExceptionHandlers.BaseExceptionHandler.getErrorMsg(e5);+ Assertions.assertEquals("test2", msg5);++ String msg6 = ExceptionHandlers.BaseExceptionHandler.getErrorMsg(e6);+ Assertions.assertEquals("", msg6);+ }+}