mapRouteResultPF

Signature

def mapRouteResultPF(f: PartialFunction[RouteResult, RouteResult]): Directive0 

Description

Partial Function version of mapRouteResult.

Changes the message the inner route sends to the responder.

The mapRouteResult directive is used as a building block for Custom Directives to transform the RouteResult coming back from the inner route. It’s similar to the mapRouteResult directive but allows to specify a partial function that doesn’t have to handle all potential RouteResultRouteResult instances.

See Result Transformation Directives for similar directives.

Example

Scala
sourcecase object MyCustomRejection extends Rejection
val rejectRejections = // not particularly useful directive
  mapRouteResultPF {
    case Rejected(_) => Rejected(List(AuthorizationFailedRejection))
  }
val route =
  rejectRejections {
    reject(MyCustomRejection)
  }

// tests:
Get("/") ~> route ~> check {
  rejection shouldEqual AuthorizationFailedRejection
}
Java
sourceimport static org.apache.pekko.http.javadsl.server.Directives.mapRouteResultPF;

import static org.apache.pekko.http.javadsl.server.Directives.mapRouteResultPF;
import static org.apache.pekko.http.javadsl.server.Directives.reject;

final Route route =
    mapRouteResultPF(
        new PFBuilder<RouteResult, RouteResult>()
            .match(
                Rejected.class,
                rejected -> {
                  final Iterable<Rejection> rejections =
                      Collections.singletonList(Rejections.authorizationFailed());
                  return RouteResults.rejected(rejections);
                })
            .build(),
        () -> reject(new MyCustomRejection()));

// tests:
runRouteUnSealed(route, HttpRequest.GET("/"))
    .assertRejections(Rejections.authorizationFailed());