mapRouteResultFuture

mapRouteResultFuture

Signature

Description

Asynchronous version of mapRouteResult.

It's similar to mapRouteResultWith, however it's Future[RouteResult] Future[RouteResult] instead of RouteResult Future[RouteResult] which may be useful when combining multiple transformantions and / or wanting to recover from a failed route result.

See Transforming the RouteResult for similar directives.

Example

val tryRecoverAddServer = mapRouteResultFuture { fr =>
  fr recover {
    case ex: IllegalArgumentException =>
      Complete(HttpResponse(StatusCodes.InternalServerError))
  } map {
    case Complete(res) => Complete(res.addHeader(Server("MyServer 1.0")))
    case rest          => rest
  }
}

val route =
  tryRecoverAddServer {
    complete("Hello world!")
  }

// tests:
Get("/") ~> route ~> check {
  status shouldEqual StatusCodes.OK
  header[Server] shouldEqual Some(Server("MyServer 1.0"))
}

Contents