})).failureHandler(routingContext -> { SQLConnection conn = routingContext.get("conn"); if (conn != null) {
.failureHandler((routingContext) -> { Throwable failure = routingContext.failure(); if (failure instanceof ValidationException) {
@Override public Router mountSubRouter(String mountPoint, Router subRouter) { if (mountPoint.endsWith("*")) { throw new IllegalArgumentException("Don't include * when mounting subrouter"); } if (mountPoint.contains(":")) { throw new IllegalArgumentException("Can't use patterns in subrouter mounts"); } route(mountPoint + "*").handler(subRouter::handleContext).failureHandler(subRouter::handleFailure); return this; }
@Test public void testFailureinHandlingFailure() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }).failureHandler(frc -> { throw new RuntimeException("super ouch!"); }); testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
@Override public void setUp() throws Exception { super.setUp(); router.route().failureHandler(ErrorHandler.create()); }
@Override public void init(Router router) { router.routeWithRegex(PATTERN_ANY).handler(CookieHandler.create()); router.routeWithRegex(PATTERN_ANY).handler(createBodyHandler()); router.routeWithRegex(PATTERN_ANY).failureHandler(this::onFailure).handler(this::onRequest); }
@Override public void init(Router router) { router.route().handler(CookieHandler.create()); router.route().handler(createBodyHandler()); router.route().failureHandler(this::failureHandler).handler(this::onRequest); }
@Test public void testFailureinHandlingFailureWithInvalidStatusMessage() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }).failureHandler(frc -> frc.response().setStatusMessage("Hello\nWorld").end()); testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
@Test public void testFailureHandler1() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }).failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); testRequest(HttpMethod.GET, path, 555, "oh dear"); }
@Test public void testFailureHandlerNoMatch() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }); router.route("/other").failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); // Default failure response testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
@Test public void testFailureHandler2() throws Exception { String path = "/blah"; router.route(path).handler(rc -> { throw new RuntimeException("ouch!"); }); router.route("/bl*").failureHandler(frc -> frc.response().setStatusCode(555).setStatusMessage("oh dear").end()); testRequest(HttpMethod.GET, path, 555, "oh dear"); }
@Test public void testSubRouteRegex() throws Exception { Router subRouter = Router.router(vertx); router.routeWithRegex("/foo/.*").handler(subRouter::handleContext).failureHandler(subRouter::handleFailure); subRouter.route("/blah").handler(rc -> rc.response().setStatusMessage("sausages").end()); testRequest(HttpMethod.GET, "/foo/blah", 500, "Internal Server Error"); }
@Test public void testFailureWithThrowable() throws Exception { String path = "/blah"; Throwable failure = new Throwable(); router.route(path).handler(rc -> rc.fail(failure)).failureHandler(frc -> { assertEquals(-1, frc.statusCode()); assertSame(failure, frc.failure()); frc.response().setStatusCode(500).setStatusMessage("Internal Server Error").end(); }); testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
@Test public void tesHandledRuntimeException2() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo/*").handler(rc -> { throw new RuntimeException("Balderdash!"); }); subRouter.route("/foo/*").failureHandler(rc -> { assertEquals(-1, rc.statusCode()); assertEquals("Balderdash!", rc.failure().getMessage()); rc.response().setStatusCode(555).setStatusMessage("Badgers").end(); }); testRequest(HttpMethod.GET, "/subpath/foo/bar", 555, "Badgers"); }
@Test public void testFailureHandler1CallFail() throws Exception { String path = "/blah"; router.route(path).handler(rc -> rc.fail(400)).failureHandler(frc -> { assertEquals(400, frc.statusCode()); frc.response().setStatusCode(400).setStatusMessage("oh dear").end(); }); testRequest(HttpMethod.GET, path, 400, "oh dear"); }
@Test public void testChangeOrderAfterActive2() throws Exception { String path = "/blah"; Route route = router.route(path).failureHandler(rc -> { rc.response().write("apples"); rc.next(); }); try { route.order(23); fail(); } catch (IllegalStateException e) { // OK } }
@Test public void testFailureHandler2CallFail() throws Exception { String path = "/blah"; router.route(path).handler(rc -> rc.fail(400)); router.route("/bl*").failureHandler(frc -> { assertEquals(400, frc.statusCode()); frc.response().setStatusCode(400).setStatusMessage("oh dear").end(); }); testRequest(HttpMethod.GET, path, 400, "oh dear"); }
@Test public void testFailureWithNullThrowable() throws Exception { String path = "/blah"; router.route(path).handler(rc -> rc.fail(null)).failureHandler(frc -> { assertEquals(-1, frc.statusCode()); assertTrue(frc.failure() instanceof NullPointerException); frc.response().setStatusCode(500).setStatusMessage("Internal Server Error").end(); }); testRequest(HttpMethod.GET, path, 500, "Internal Server Error"); }
@Test public void testFailCalled1() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo/*").handler(rc -> rc.fail(557)); router.route("/subpath/*").failureHandler(rc -> { assertEquals(557, rc.statusCode()); assertNull(rc.failure()); rc.response().setStatusCode(rc.statusCode()).setStatusMessage("Chipmunks").end(); }); testRequest(HttpMethod.GET, "/subpath/foo/bar", 557, "Chipmunks"); }
@Test public void testFailCalled2() throws Exception { Router subRouter = Router.router(vertx); router.mountSubRouter("/subpath", subRouter); subRouter.route("/foo/*").handler(rc -> rc.fail(557)); router.route("/subpath/*").failureHandler(rc -> { assertEquals(557, rc.statusCode()); assertNull(rc.failure()); rc.response().setStatusCode(rc.statusCode()).setStatusMessage("Chipmunks").end(); }); testRequest(HttpMethod.GET, "/subpath/foo/bar", 557, "Chipmunks"); }