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

[#1551] improve(server): Optimize the error message #1620

Merged
merged 4 commits into from
Jan 24, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();

Expand All @@ -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());
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading