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.java index 1a8b9059f2d..99b973b7f86 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.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 @@ private static class TableExceptionHandler extends BaseExceptionHandler { 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 @@ private static class SchemaExceptionHandler extends BaseExceptionHandler { 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 @@ private static class CatalogExceptionHandler extends BaseExceptionHandler { 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 @@ private static class MetalakeExceptionHandler extends BaseExceptionHandler { @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()); + 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 Response handle(OperationType op, String metalake, String parent, Excepti } } - 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 Response handle(OperationType op, String object, String parent, Exception 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.java new file mode 100644 index 00000000000..54a057ebbd1 --- /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); + } +}