handleExceptions
Catches exceptions thrown by the inner route and handles them using the specified ExceptionHandler.
Signature
def handleExceptions(handler: ExceptionHandler): Directive0
Description
Using this directive is an alternative to using a global implicitly defined ExceptionHandler that applies to the complete route.
See Exception Handling for general information about options for handling exceptions.
Example
val divByZeroHandler = ExceptionHandler {
case _: ArithmeticException => complete(StatusCodes.BadRequest, "You've got your arithmetic wrong, fool!")
}
val route =
path("divide" / IntNumber / IntNumber) { (a, b) =>
handleExceptions(divByZeroHandler) {
complete(s"The result is ${a / b}")
}
}
Get("/divide/10/5") ~> route ~> check {
responseAs[String] === "The result is 2"
}
Get("/divide/10/0") ~> route ~> check {
status === StatusCodes.BadRequest
responseAs[String] === "You've got your arithmetic wrong, fool!"
}