Skip to content

Commit

Permalink
[apache#1551] improve(server): Optimize the error message (apache#1620)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

This PR optimize the error message to make it more meaningful to the UI.

Previously it outputs the exception class name, which is not meaningful
to users. Now change to the actual error message like below:

```
Failed to operate table(s) [ADMINISTRABLE_ROLE_AUTHORIZATIONS] operation [LOAD] under schema [information_schema], reason [Failed to parse create table information_schema.ADMINISTRABLE_ROLE_AUTHORIZATIONS SQL]
```

The reason field is only the error message now.


### Why are the changes needed?

To make the error message on UI more meaningful.

Fix: apache#1551 

### Does this PR introduce _any_ user-facing change?

No.

### How was this patch tested?

Local verify.
  • Loading branch information
jerryshao authored and mchades committed Jan 24, 2024
1 parent 34faff9 commit b8fa890
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
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);
}
}

0 comments on commit b8fa890

Please sign in to comment.