mapRejections • Akka HTTP (original) (raw)

Signature

def mapRejections(f: immutable.Seq[Rejection] => immutable.Seq[Rejection]): Directive0

Description

Low level directive – unless you’re sure you need to be working on this low-level you might instead want to try the handleRejections directive which provides a nicer DSL for building rejection handlers.

The mapRejections directive is used as a building block for Custom Directives to transform a list of rejections from the inner route to a new list of rejections.

See Response Transforming Directives for similar directives.

Example

Scala

source`// ignore any rejections and replace them by AuthorizationFailedRejection val replaceByAuthorizationFailed = mapRejections(_ => List(AuthorizationFailedRejection)) val route = replaceByAuthorizationFailed { path("abc")(complete("abc")) }

// tests: Get("/") ~> route ~> check { rejection shouldEqual AuthorizationFailedRejection }

Get("/abc") ~> route ~> check { status shouldEqual StatusCodes.OK }`

Java

source`import static akka.http.javadsl.server.Directives.mapRejections;

// ignore any rejections and replace them by AuthorizationFailedRejection final Route route = mapRejections( rejections -> Collections.singletonList((Rejection) Rejections.authorizationFailed()), () -> path("abc", () -> complete("abc")) );

// tests: runRouteUnSealed(route, HttpRequest.GET("/")) .assertRejections(Rejections.authorizationFailed()); testRoute(route).run(HttpRequest.GET("/abc")) .assertStatusCode(StatusCodes.OK);`

Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.