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