onComplete

onComplete

Description

Evaluates its parameter of type CompletionStage<T>, and once it has been completed, extracts its result as a value of type Try<T> and passes it to the inner route. A Try<T> can either be a Success containing the T value or a Failure containing the Throwable.

To handle the Failure case automatically and only work with the result value, use onSuccess.

To complete with a successful result automatically and just handle the failure result, use completeOrRecoverWith, instead.

Example

// import static scala.compat.java8.JFunction.func;
// import static akka.http.javadsl.server.PathMatchers.*;

final Route route = path(segment("divide").slash(integerSegment()).slash(integerSegment()),
  (a, b) -> onComplete(
    () -> CompletableFuture.supplyAsync(() -> a / b),
    maybeResult -> maybeResult
      .map(func(result -> complete("The result was " + result)))
      .recover(new PFBuilder<Throwable, Route>()
        .matchAny(ex -> complete(StatusCodes.InternalServerError(),
          "An error occurred: " + ex.getMessage())
        )
        .build())
      .get()
  )
);

testRoute(route).run(HttpRequest.GET("/divide/10/2"))
  .assertEntity("The result was 5");

testRoute(route).run(HttpRequest.GET("/divide/10/0"))
  .assertStatusCode(StatusCodes.InternalServerError())
  .assertEntity("An error occurred: / by zero");

Contents