mapRouteResultFuture

Signature

def mapRouteResultFuture(f: Future[RouteResult] ⇒ Future[RouteResult]): Directive0

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 transformations and / or wanting to recover from a failed route result.

See Result Transformation Directives 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"))
}
The source code for this page can be found here.