failWith
Bubbles up the given error through the route structure where it is dealt with by the closest handleExceptions
directive and its ExceptionHandler.
Signature
def failWith(error: Throwable): StandardRoute
Description
failWith explicitly raises an exception that gets bubbled up through the route structure to be picked up by the
nearest handleExceptions directive. If no handleExceptions is present above the respective location in the
route structure The runRoute Wrapper will handle the exception and translate it into a corresponding HttpResponse using
the in-scope ExceptionHandler (see also the Exception Handling chapter).
There is one notable special case: If the given exception is a RejectionError exception it is not bubbled up,
but rather the wrapped exception is unpacked and “executed”. This allows the “tunneling” of a rejection via an
exception.
Example
val route =
path("foo") {
failWith(new RequestProcessingException(StatusCodes.BandwidthLimitExceeded))
}
Get("/foo") ~> sealRoute(route) ~> check {
status === StatusCodes.BandwidthLimitExceeded
responseAs[String] === "Bandwidth limit has been exceeded."
}