handleRejections
§Signature
§Description
Using this directive is an alternative to using a global implicitly defined RejectionHandler
that
applies to the complete route.
See Rejections for general information about options for handling rejections.
§Example
- val totallyMissingHandler = RejectionHandler.newBuilder()
- .handleNotFound { complete((StatusCodes.NotFound, "Oh man, what you are looking for is long gone.")) }
- .handle { case ValidationRejection(msg, _) => complete((StatusCodes.InternalServerError, msg)) }
- .result()
- val route =
- pathPrefix("handled") {
- handleRejections(totallyMissingHandler) {
- path("existing")(complete("This path exists")) ~
- path("boom")(reject(new ValidationRejection("This didn't work.")))
- }
- }
-
- // tests:
- Get("/handled/existing") ~> route ~> check {
- responseAs[String] shouldEqual "This path exists"
- }
- Get("/missing") ~> Route.seal(route) /* applies default handler */ ~> check {
- status shouldEqual StatusCodes.NotFound
- responseAs[String] shouldEqual "The requested resource could not be found."
- }
- Get("/handled/missing") ~> route ~> check {
- status shouldEqual StatusCodes.NotFound
- responseAs[String] shouldEqual "Oh man, what you are looking for is long gone."
- }
- Get("/handled/boom") ~> route ~> check {
- status shouldEqual StatusCodes.InternalServerError
- responseAs[String] shouldEqual "This didn't work."
- }
Contents