4747import io .mantisrx .server .master .resourcecluster .RequestThrottledException ;
4848import io .mantisrx .server .master .resourcecluster .ResourceCluster .TaskExecutorNotFoundException ;
4949import io .mantisrx .server .master .resourcecluster .TaskExecutorTaskCancelledException ;
50+ import io .mantisrx .shaded .com .fasterxml .jackson .databind .JsonNode ;
5051import io .mantisrx .shaded .com .fasterxml .jackson .databind .node .JsonNodeFactory ;
5152import io .mantisrx .shaded .com .fasterxml .jackson .databind .node .ObjectNode ;
5253import 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 }
0 commit comments