recoverRejectionsWith

recoverRejectionsWith

§Signature

§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.

Transforms rejections from the inner route with an immutable.Seq[Rejection] Future[RouteResult] function.

Asynchronous version of recoverRejections.

See recoverRejections (the synchronous equivalent of this directive) for a detailed description.

注釈

To learn more about how and why rejections work read the Rejections section of the documentation.

§Example

  1. val authRejectionsToNothingToSeeHere = recoverRejectionsWith { rejections =>
  2. Future {
  3. // imagine checking rejections takes a longer time:
  4. if (rejections.exists(_.isInstanceOf[AuthenticationFailedRejection]))
  5. Complete(HttpResponse(entity = "Nothing to see here, move along."))
  6. else
  7. Rejected(rejections)
  8. }
  9. }
  10. val neverAuth: Authenticator[String] = creds => None
  11.  
  12. val route =
  13. authRejectionsToNothingToSeeHere {
  14. pathPrefix("auth") {
  15. path("never") {
  16. authenticateBasic("my-realm", neverAuth) { user =>
  17. complete("Welcome to the bat-cave!")
  18. }
  19. }
  20. }
  21. }
  22.  
  23. // tests:
  24. Get("/auth/never") ~> route ~> check {
  25. status shouldEqual StatusCodes.OK
  26. responseAs[String] shouldEqual "Nothing to see here, move along."
  27. }