Skip to content

Commit 4cf73d5

Browse files
author
ggao
committed
Fix the error message of the exception handle
1 parent 0e7ae19 commit 4cf73d5

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

mantis-control-plane/mantis-control-plane-core/src/main/java/io/mantisrx/server/master/resourcecluster/TaskExecutorTaskCancelledException.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package io.mantisrx.server.master.resourcecluster;
1818

1919
import io.mantisrx.server.core.domain.WorkerId;
20+
21+
import io.mantisrx.shaded.com.fasterxml.jackson.databind.JsonNode;
22+
import io.mantisrx.shaded.com.fasterxml.jackson.databind.ObjectMapper;
2023
import lombok.Getter;
2124

2225
/**
@@ -25,6 +28,7 @@
2528
@Getter
2629
public class TaskExecutorTaskCancelledException extends Exception {
2730
private static final long serialVersionUID = 1L;
31+
private static final ObjectMapper mapper = new ObjectMapper();
2832

2933
private final WorkerId workerId;
3034

@@ -38,4 +42,12 @@ public synchronized Throwable fillInStackTrace() {
3842
// Do not include stack trace to be returned to clients
3943
return this;
4044
}
45+
46+
/**
47+
* Serializes the full exception object including workerId to JsonNode.
48+
* @return JsonNode representation of this exception
49+
*/
50+
public JsonNode toJsonNode() {
51+
return mapper.valueToTree(this);
52+
}
4153
}

mantis-control-plane/mantis-control-plane-server/src/main/java/io/mantisrx/master/api/akka/route/v1/BaseRoute.java

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import io.mantisrx.server.master.resourcecluster.RequestThrottledException;
4848
import io.mantisrx.server.master.resourcecluster.ResourceCluster.TaskExecutorNotFoundException;
4949
import io.mantisrx.server.master.resourcecluster.TaskExecutorTaskCancelledException;
50+
import io.mantisrx.shaded.com.fasterxml.jackson.databind.JsonNode;
5051
import io.mantisrx.shaded.com.fasterxml.jackson.databind.node.JsonNodeFactory;
5152
import io.mantisrx.shaded.com.fasterxml.jackson.databind.node.ObjectNode;
5253
import io.mantisrx.shaded.com.fasterxml.jackson.databind.ser.FilterProvider;
@@ -304,6 +305,15 @@ protected String generateFailureResponsePayload(String errorMsg, long requestId)
304305
return node.toString();
305306
}
306307

308+
protected String generateFailureResponsePayload(JsonNode errorMsgNode, long requestId) {
309+
ObjectNode node = JsonNodeFactory.instance.objectNode();
310+
node.put("time", System.currentTimeMillis());
311+
node.put("host", this.hostName);
312+
node.set("error", errorMsgNode);
313+
node.put("requestId", requestId);
314+
return node.toString();
315+
}
316+
307317
FilterProvider parseFilter(String fields, String target) {
308318
if (Strings.isNullOrEmpty(fields)) {
309319
return null;
@@ -351,18 +361,31 @@ protected <T> Route withFuture(CompletableFuture<T> tFuture) {
351361
throwable -> {
352362
if (throwable instanceof TaskExecutorNotFoundException) {
353363
MasterApiMetrics.getInstance().incrementResp4xx();
354-
return complete(StatusCodes.NOT_FOUND);
364+
return complete(
365+
StatusCodes.NOT_FOUND,
366+
HttpEntities.create(
367+
ContentTypes.APPLICATION_JSON,
368+
generateFailureResponsePayload(throwable.getMessage(), -1)));
355369
}
356370

357371
if (throwable instanceof RequestThrottledException) {
358372
MasterApiMetrics.getInstance().incrementResp4xx();
359373
MasterApiMetrics.getInstance().incrementThrottledRequestCount();
360-
return complete(StatusCodes.TOO_MANY_REQUESTS);
374+
return complete(
375+
StatusCodes.TOO_MANY_REQUESTS,
376+
HttpEntities.create(
377+
ContentTypes.APPLICATION_JSON,
378+
generateFailureResponsePayload(throwable.getMessage(), -1)));
361379
}
362380

363381
if (throwable instanceof TaskExecutorTaskCancelledException) {
364382
MasterApiMetrics.getInstance().incrementResp4xx();
365-
return complete(StatusCodes.NOT_ACCEPTABLE, throwable, Jackson.marshaller() );
383+
TaskExecutorTaskCancelledException ex = (TaskExecutorTaskCancelledException) throwable;
384+
return complete(
385+
StatusCodes.NOT_ACCEPTABLE,
386+
HttpEntities.create(
387+
ContentTypes.APPLICATION_JSON,
388+
generateFailureResponsePayload(ex.toJsonNode(), -1)));
366389
}
367390

368391
if (throwable instanceof AskTimeoutException) {
@@ -371,7 +394,11 @@ protected <T> Route withFuture(CompletableFuture<T> tFuture) {
371394

372395
MasterApiMetrics.getInstance().incrementResp5xx();
373396
logger.error("withFuture error: ", throwable);
374-
return complete(StatusCodes.INTERNAL_SERVER_ERROR, throwable, Jackson.marshaller());
397+
return complete(
398+
StatusCodes.INTERNAL_SERVER_ERROR,
399+
HttpEntities.create(
400+
ContentTypes.APPLICATION_JSON,
401+
generateFailureResponsePayload(throwable.getMessage(), -1)));
375402
},
376403
r -> complete(StatusCodes.OK, r, Jackson.marshaller())));
377404
}

mantis-control-plane/mantis-control-plane-server/src/test/java/io/mantisrx/master/api/akka/route/v1/ResourceClustersLeaderExclusiveRouteTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
import io.mantisrx.server.master.resourcecluster.TaskExecutorReport;
3838
import io.mantisrx.server.master.resourcecluster.TaskExecutorTaskCancelledException;
3939
import java.io.IOException;
40+
41+
import io.mantisrx.shaded.com.fasterxml.jackson.databind.JsonNode;
42+
import io.mantisrx.shaded.com.fasterxml.jackson.databind.ObjectMapper;
4043
import org.junit.BeforeClass;
4144
import org.junit.Test;
4245
import org.mockito.ArgumentMatchers;
@@ -71,10 +74,21 @@ public void testGetTaskExecutorStateWithCancelledWorker() throws IOException {
7174
);
7275
String encoded = serializer.toJson(heartbeat);
7376

74-
testRoute.run(
77+
String response = testRoute.run(
7578
HttpRequest.POST("/api/v1/resourceClusters/myCluster/actions/heartBeatFromTaskExecutor")
7679
.withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, encoded)))
7780
.assertStatusCode(StatusCodes.NOT_ACCEPTABLE)
78-
.assertEntity(serializer.toJson(err));
81+
.entityString();
82+
83+
// Parse response and verify the error field contains the exception
84+
ObjectMapper mapper = new ObjectMapper();
85+
JsonNode responseNode = mapper.readTree(response);
86+
JsonNode errorNode = responseNode.get("error");
87+
88+
String expectedError = serializer.toJson(err);
89+
String actualError = mapper.writeValueAsString(errorNode);
90+
91+
assert actualError.equals(expectedError) :
92+
String.format("Expected error: %s, but got: %s", expectedError, actualError);
7993
}
8094
}

0 commit comments

Comments
 (0)