Vert.x Web failureHandler vs. errorHandler: handling 403, 404 and 500
In Vert.x Web, distinguish a failure from a matched route handler from a request that does not match a route. Use Route.failureHandler for failures scoped by path and method; use Router.errorHandler for a response tied to a status code, such as an unmatched-path 404. This Java example follows the Vert.x Web 5.2.3 documentation, reviewed on 2026-11-20.
In Vert.x Web, distinguish a failure from a matched route handler from a request that does not match a route. Use Route.failureHandler for failures scoped by path and method; use Router.errorHandler for a response tied to a status code, such as an unmatched-path 404. This Java example follows the Vert.x Web 5.2.3 documentation, reviewed on 2026-11-20.
Prerequisite
Add io.vertx:vertx-web using the version managed by your Vert.x BOM or project policy. The official 5.2.3 docs show this Maven dependency:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>5.2.3</version>
</dependency>Register route and status handlers
Create a router, attach a route-specific failure handler, then register error responses by status:
import io.vertx.core.Vertx;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
Vertx vertx = Vertx.vertx();
Router router = Router.router(vertx);
Route privateRoute = router.get("/private/*");
privateRoute.handler(ctx -> ctx.fail(403));
privateRoute.failureHandler(ctx -> {
int status = ctx.statusCode();
String body = status == 403 ? "forbidden" : "request_failed";
ctx.response()
.setStatusCode(status)
.putHeader("content-type", "text/plain; charset=utf-8")
.end(body);
});
router.errorHandler(404, ctx -> ctx.response()
.setStatusCode(404)
.putHeader("content-type", "text/plain; charset=utf-8")
.end("not_found"));
router.errorHandler(500, ctx -> ctx.response()
.setStatusCode(500)
.putHeader("content-type", "text/plain; charset=utf-8")
.end("internal_error"));
vertx.createHttpServer()
.requestHandler(router)
.listen(8080);The route matches GET requests under /private/*. Calling ctx.fail(403) enters failure routing; the failure handler on the same route reads the status and ends the response. A matching handler exception can also enter failure routing as 500, so produce a safe body for the actual status.
Router.errorHandler(404, ...) handles the no-matching-path case. Router.errorHandler(500, ...) can provide a status-specific fallback when other failure handlers did not write a response or an exception is otherwise unhandled. Do not expose exception messages or implementation details in client responses.
Verify responses
After starting the server, send requests from another terminal:
curl -i http://localhost:8080/private/report
curl -i http://localhost:8080/does-not-existExpected results are status 403 with body forbidden, then status 404 with body not_found. If a missing path returns the default response, check that the 404 error handler is registered. If /private/report returns 404, check the route's method and path pattern.
Identify the failure category
Vert.x Web documents these route-match failures:
| Status | Match failure |
|---|---|
| 404 | No path matches |
| 405 | Path matches, HTTP method does not |
| 406 | No response media type matches the request's Accept |
| 415 | Request Content-Type cannot be consumed |
| 400 | The matched route cannot accept an empty body |
Use Router.errorHandler(statusCode, handler) to define responses for these status codes. In contrast, Route.failureHandler handles a request that entered failure routing because a matched handler called ctx.fail(...) or threw, under the route's matching criteria. A normal handler that already ended its response has not handed control to a later failure response.
Do not call RoutingContext.next() inside Router.errorHandler; the API documentation says this handler should finish the error response rather than continue ordinary failure routing.
Official sources
[Vert.x Web 5.2.3 Java docs](https://vertx.io/docs/vertx-web/java/)
[Vert.x Router API](https://vertx.io/docs/apidocs/io/vertx/ext/web/Router.html)
[Vert.x Route API](https://vertx.io/docs/apidocs/io/vertx/ext/web/Route.html)
[Vert.x RoutingContext API](https://vertx.io/docs/apidocs/io/vertx/ext/web/RoutingContext.html)