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

Prevent misleading warning messages with HK2 MultiException #492

Merged
merged 1 commit into from
Apr 30, 2019
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 @@ -32,13 +32,22 @@
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

import org.glassfish.hk2.api.MultiException;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.config.ServerOptions;
import com.baidu.hugegraph.exception.NotFoundException;

public class ExceptionFilter {

private static final int BAD_REQUEST_ERROR =
Response.Status.BAD_REQUEST.getStatusCode();
private static final int NOT_FOUND_ERROR =
Response.Status.NOT_FOUND.getStatusCode();
private static final int INTERNAL_SERVER_ERROR =
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();

public static class TracedExceptionMapper {

@Context
Expand All @@ -59,7 +68,7 @@ public static class HugeExceptionMapper

@Override
public Response toResponse(HugeException exception) {
return Response.status(400)
return Response.status(BAD_REQUEST_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception))
.build();
Expand All @@ -72,7 +81,7 @@ public static class IllegalArgumentExceptionMapper

@Override
public Response toResponse(IllegalArgumentException exception) {
return Response.status(400)
return Response.status(BAD_REQUEST_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception))
.build();
Expand All @@ -85,7 +94,7 @@ public static class NotFoundExceptionMapper

@Override
public Response toResponse(NotFoundException exception) {
return Response.status(404)
return Response.status(NOT_FOUND_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception))
.build();
Expand All @@ -98,7 +107,7 @@ public static class NoSuchElementExceptionMapper

@Override
public Response toResponse(NoSuchElementException exception) {
return Response.status(404)
return Response.status(NOT_FOUND_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception))
.build();
Expand All @@ -110,9 +119,6 @@ public static class WebApplicationExceptionMapper
extends TracedExceptionMapper
implements ExceptionMapper<WebApplicationException> {

private static final int INTERNAL_SERVER_ERROR =
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();

@Override
public Response toResponse(WebApplicationException exception) {
Response response = exception.getResponse();
Expand All @@ -136,22 +142,26 @@ private boolean trace(int status) {

@Provider
public static class UnknownExceptionMapper extends TracedExceptionMapper
implements ExceptionMapper<Exception> {
implements ExceptionMapper<Throwable> {

@Override
public Response toResponse(Exception exception) {
return Response.status(500)
public Response toResponse(Throwable exception) {
if (exception instanceof MultiException &&
((MultiException) exception).getErrors().size() == 1) {
exception = ((MultiException) exception).getErrors().get(0);
}
return Response.status(INTERNAL_SERVER_ERROR)
.type(MediaType.APPLICATION_JSON)
.entity(formatException(exception, this.trace()))
.build();
}
}

public static String formatException(Exception exception) {
public static String formatException(Throwable exception) {
return formatException(exception, false);
}

public static String formatException(Exception exception, boolean trace) {
public static String formatException(Throwable exception, boolean trace) {
String clazz = exception.getClass().toString();
String msg = exception.getMessage() != null ?
exception.getMessage() : "";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ private void restoreUncompletedTasks() {
for (String graph : this.graphs()) {
HugeGraph hugegraph = this.graph(graph);
assert hugegraph != null;
LOG.info("Restoring incomplete tasks for graph '{}'...", graph);
hugegraph.taskScheduler().restoreTasks();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import org.apache.tinkerpop.gremlin.server.util.MetricManager;
import org.glassfish.hk2.api.Factory;
import org.glassfish.hk2.api.MultiException;
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.ResourceConfig;
Expand All @@ -32,6 +33,7 @@
import org.glassfish.jersey.server.monitoring.RequestEvent;
import org.glassfish.jersey.server.monitoring.RequestEventListener;

import com.baidu.hugegraph.HugeException;
import com.baidu.hugegraph.config.HugeConfig;
import com.baidu.hugegraph.core.GraphManager;
import com.baidu.hugegraph.core.WorkLoad;
Expand Down Expand Up @@ -121,8 +123,10 @@ protected void configure() {

@Override
public GraphManager provide() {
E.checkState(this.manager != null,
"Please wait for the server to initialize");
if (this.manager == null) {
String message = "Please wait for the server to initialize";
throw new MultiException(new HugeException(message), false);
}
return this.manager;
}

Expand Down