mapRouteResultFuture

mapRouteResultFuture

Description

Asynchronous version of mapRouteResult.

It's similar to mapRouteResultWith, however it's Function<CompletionStage<RouteResult>, CompletionStage<RouteResult>> instead of Function<RouteResult, CompletionStage<RouteResult>> which may be useful when combining multiple transformations and / or wanting to recover from a failed route result.

See Transforming the RouteResult for similar directives.

Example

final Route route = mapRouteResultFuture(cr ->
  cr.exceptionally(t -> {
    if (t instanceof IllegalArgumentException) {
      return RouteResults.complete(
        HttpResponse.create().withStatus(StatusCodes.INTERNAL_SERVER_ERROR));
    } else {
      return null;
    }
  }).thenApply(rr -> {
    if (rr instanceof Complete) {
      final HttpResponse res = ((Complete) rr).getResponse();
      return RouteResults.complete(
        res.addHeader(Server.create(ProductVersion.create("MyServer", "1.0"))));
    } else {
      return rr;
    }
  }), () -> complete("Hello world!"));

// tests:
testRoute(route).run(HttpRequest.GET("/"))
  .assertStatusCode(StatusCodes.OK)
  .assertHeaderExists(Server.create(ProductVersion.create("MyServer", "1.0")));

Contents