Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,19 @@ boolean iterateNext() {
currentRoute.handleFailure(this);
return true;
}

RouterImpl subRouter = (RouterImpl) currentRoute.getSubRouter();
if (subRouter != null) {
int statusCode = statusCode();
if (statusCode == -1) {
statusCode = failed ? 500 : matchFailure;
}
Handler<RoutingContext> errorHandler = subRouter.getErrorHandlerByStatusCode(statusCode);
if (errorHandler != null) {
errorHandler.handle(this);
return true;
}
}
} catch (Throwable t) {
handleInHandlerRuntimeFailure(currentRoute.getRouter(), failed, t);
return true;
Expand Down
41 changes: 41 additions & 0 deletions vertx-web/src/test/java/io/vertx/ext/web/tests/SubRouterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.vertx.ext.web.tests;

import io.netty.handler.codec.http.HttpResponseStatus;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
Expand All @@ -25,6 +26,9 @@

import java.util.function.Consumer;

import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;

/**
* @author <a href="http://tfox.org">Tim Fox</a>
*/
Expand Down Expand Up @@ -743,4 +747,41 @@ public void testHierarchicalWithParamsSimpleWithDummy() throws Exception {

testRequest(HttpMethod.GET, "/rest/product/123/bar", 200, "OK");
}

@Test
public void testSetExceptionHandler() throws Exception {
Router restRouter = Router.router(vertx);
Router productRouter = Router.router(vertx);
Router instanceRouter = Router.router(vertx);

router.route("/rest*").subRouter(restRouter);
restRouter.route("/product*").subRouter(productRouter);
productRouter.route("/:id*").subRouter(instanceRouter);
instanceRouter.get("/:foo").handler(ctx -> {
if ("ex".equals(ctx.pathParam("foo"))) {
throw new RuntimeException("ouch!");
}
ctx.response().end();
});

testRequest(HttpMethod.GET, "/rest/product/123/ex", 500, "Internal Server Error", "Internal Server Error");

assertRouterErrorHandlers("root", router, INTERNAL_SERVER_ERROR, "/rest/product/123/ex");
assertRouterErrorHandlers("root", router, NOT_FOUND, "/rest/product/123/foo/404");

assertRouterErrorHandlers("rest", restRouter, INTERNAL_SERVER_ERROR, "/rest/product/123/ex");
assertRouterErrorHandlers("rest", restRouter, NOT_FOUND, "/rest/product/123/foo/404");

assertRouterErrorHandlers("product", productRouter, INTERNAL_SERVER_ERROR, "/rest/product/123/ex");
assertRouterErrorHandlers("product", productRouter, NOT_FOUND, "/rest/product/123/foo/404");

assertRouterErrorHandlers("instance", instanceRouter, INTERNAL_SERVER_ERROR, "/rest/product/123/ex");
assertRouterErrorHandlers("instance", instanceRouter, NOT_FOUND, "/rest/product/123/foo/404");
}

private void assertRouterErrorHandlers(String name, Router router, HttpResponseStatus status, String path) throws Exception {
String handlerKey = name + "." + status.codeAsText() + ".errorHandler";
router.errorHandler(status.code(), ctx -> ctx.response().setStatusCode(status.code()).end(handlerKey));
testRequest(HttpMethod.GET, path, status.code(), status.reasonPhrase(), handlerKey);
}
}
Loading