cancelRejection

Signature

def cancelRejection(rejection: Rejection): Directive0 

Description

Adds a TransformationRejectionTransformationRejection cancelling all rejections equal to the given one to the rejections potentially coming back from the inner route.

Read Rejections to learn more about rejections.

For more advanced handling of rejections refer to the handleRejections directive which provides a nicer DSL for building rejection handlers.

Example

Scala
sourceval route =
  cancelRejection(MethodRejection(HttpMethods.POST)) {
    post {
      complete("Result")
    }
  }

// tests:
Get("/") ~> route ~> check {
  rejections shouldEqual Nil
  handled shouldEqual false
}
Java
sourceimport static org.apache.pekko.http.javadsl.server.Directives.cancelRejection;

final Route route =
    cancelRejection(Rejections.method(HttpMethods.POST), () -> post(() -> complete("Result")));

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