handleExceptions
Description
Catches exceptions thrown by the inner route and handles them using the specified ExceptionHandler
.
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
final ExceptionHandler divByZeroHandler = ExceptionHandler.newBuilder()
.match(ArithmeticException.class, x ->
complete(StatusCodes.BAD_REQUEST, "You've got your arithmetic wrong, fool!"))
.build();
final Route route =
path(PathMatchers.segment("divide").slash(integerSegment()).slash(integerSegment()), (a, b) ->
handleExceptions(divByZeroHandler, () -> complete("The result is " + (a / b)))
);
// tests:
testRoute(route).run(HttpRequest.GET("/divide/10/5"))
.assertEntity("The result is 2");
testRoute(route).run(HttpRequest.GET("/divide/10/0"))
.assertStatusCode(StatusCodes.BAD_REQUEST)
.assertEntity("You've got your arithmetic wrong, fool!");