|
16 | 16 |
|
17 | 17 | package io.vertx.ext.web; |
18 | 18 |
|
| 19 | +import io.netty.handler.codec.http.HttpResponseStatus; |
19 | 20 | import io.vertx.core.Handler; |
20 | 21 | import io.vertx.core.Vertx; |
21 | 22 | import io.vertx.core.http.HttpMethod; |
22 | 23 | import org.junit.Test; |
23 | 24 |
|
24 | 25 | import java.util.function.Consumer; |
25 | 26 |
|
| 27 | +import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR; |
| 28 | +import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND; |
| 29 | + |
26 | 30 | /** |
27 | 31 | * @author <a href="http://tfox.org">Tim Fox</a> |
28 | 32 | */ |
@@ -741,4 +745,41 @@ public void testHierarchicalWithParamsSimpleWithDummy() throws Exception { |
741 | 745 |
|
742 | 746 | testRequest(HttpMethod.GET, "/rest/product/123/bar", 200, "OK"); |
743 | 747 | } |
| 748 | + |
| 749 | + @Test |
| 750 | + public void testSetExceptionHandler() throws Exception { |
| 751 | + Router restRouter = Router.router(vertx); |
| 752 | + Router productRouter = Router.router(vertx); |
| 753 | + Router instanceRouter = Router.router(vertx); |
| 754 | + |
| 755 | + router.route("/rest*").subRouter(restRouter); |
| 756 | + restRouter.route("/product*").subRouter(productRouter); |
| 757 | + productRouter.route("/:id*").subRouter(instanceRouter); |
| 758 | + instanceRouter.get("/:foo").handler(ctx -> { |
| 759 | + if ("ex".equals(ctx.pathParam("foo"))) { |
| 760 | + throw new RuntimeException("ouch!"); |
| 761 | + } |
| 762 | + ctx.response().end(); |
| 763 | + }); |
| 764 | + |
| 765 | + testRequest(HttpMethod.GET, "/rest/product/123/ex", 500, "Internal Server Error", "Internal Server Error"); |
| 766 | + |
| 767 | + assertRouterErrorHandlers("root", router, INTERNAL_SERVER_ERROR, "/rest/product/123/ex"); |
| 768 | + assertRouterErrorHandlers("root", router, NOT_FOUND, "/rest/product/123/foo/404"); |
| 769 | + |
| 770 | + assertRouterErrorHandlers("rest", restRouter, INTERNAL_SERVER_ERROR, "/rest/product/123/ex"); |
| 771 | + assertRouterErrorHandlers("rest", restRouter, NOT_FOUND, "/rest/product/123/foo/404"); |
| 772 | + |
| 773 | + assertRouterErrorHandlers("product", productRouter, INTERNAL_SERVER_ERROR, "/rest/product/123/ex"); |
| 774 | + assertRouterErrorHandlers("product", productRouter, NOT_FOUND, "/rest/product/123/foo/404"); |
| 775 | + |
| 776 | + assertRouterErrorHandlers("instance", instanceRouter, INTERNAL_SERVER_ERROR, "/rest/product/123/ex"); |
| 777 | + assertRouterErrorHandlers("instance", instanceRouter, NOT_FOUND, "/rest/product/123/foo/404"); |
| 778 | + } |
| 779 | + |
| 780 | + private void assertRouterErrorHandlers(String name, Router router, HttpResponseStatus status, String path) throws Exception { |
| 781 | + String handlerKey = name + "." + status.codeAsText() + ".errorHandler"; |
| 782 | + router.errorHandler(status.code(), ctx -> ctx.response().setStatusCode(status.code()).end(handlerKey)); |
| 783 | + testRequest(HttpMethod.GET, path, status.code(), status.reasonPhrase(), handlerKey); |
| 784 | + } |
744 | 785 | } |
0 commit comments