@RunWith(DefaultServer.class)
public class ExceptionHandlerTestCase {
@Test
public void testExceptionMappers() throws IOException {
HttpHandler pathHandler = Handlers.path()
.addExactPath("/", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("expected");
}
})
.addExactPath("/exceptionParent", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new ParentException();
}
})
.addExactPath("/exceptionChild", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new ChildException();
}
})
.addExactPath("/exceptionAnotherChild", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new AnotherChildException();
}
})
.addExactPath("/illegalArgumentException", new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
throw new IllegalArgumentException();
}
});
HttpHandler exceptionHandler = Handlers.exceptionHandler(pathHandler)
.addExceptionHandler(ChildException.class, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("child exception handled");
}
})
.addExceptionHandler(ParentException.class, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("parent exception handled");
}
})
.addExceptionHandler(Throwable.class, new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
exchange.getResponseSender().send("catch all throwables");
}
});