Vert.x Web: pagpili sa failureHandler o errorHandler para sa 403, 404, at 500
Sa Vert.x Web, pag-ibahin ang failure ng handler sa tumugmang route at ang request na walang katugmang route. Gamitin ang Route.failureHandler para sa failure na limitado sa path at method; gamitin ang Router.errorHandler para sa tugon batay sa status, gaya ng 404 para sa di-kilalang path. Sinusunod ng Java halimbawang ito ang Vert.x Web 5.2.3 docs na sinuri noong 2026-11-20.
Sa Vert.x Web, pag-ibahin ang failure ng handler sa tumugmang route at ang request na walang katugmang route. Gamitin ang Route.failureHandler para sa failure na limitado sa path at method; gamitin ang Router.errorHandler para sa tugon batay sa status, gaya ng 404 para sa di-kilalang path. Sinusunod ng Java halimbawang ito ang Vert.x Web 5.2.3 docs na sinuri noong 2026-11-20.
Kailangan
Idagdag ang io.vertx:vertx-web gamit ang bersyong pinamamahalaan ng Vert.x BOM o patakaran ng proyekto. Ipinapakita ng opisyal na 5.2.3 docs ang Maven dependency na ito:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>5.2.3</version>
</dependency>Magrehistro ng route at status handler
Gumawa ng router, ikabit ang route-specific failure handler, at magrehistro ng mga tugon ayon sa 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);Tumutugma ang route sa GET requests sa ilalim ng /private/*. Sinisimulan ng ctx.fail(403) ang failure flow; binabasa ng failure handler sa parehong route ang status at tinatapos ang tugon. Maaari ring magdulot ng 500 failure ang exception sa tumugmang handler, kaya magbalik ng ligtas na body para sa aktuwal na status.
Hinahawakan ng Router.errorHandler(404, ...) ang kawalan ng tumugmang path. Maaaring magbigay ang Router.errorHandler(500, ...) ng fallback ayon sa status kapag walang naisulat na tugon ang ibang failure handler o may exception na hindi nahawakan. Huwag ilantad sa client ang exception message o panloob na detalye.
Tiyakin ang mga tugon
Pagkatapos simulan ang server, magpadala ng requests mula sa ibang terminal:
curl -i http://localhost:8080/private/report
curl -i http://localhost:8080/does-not-existInaasahan ang status 403 na may body na forbidden, kasunod ang status 404 na may not_found. Kung default na tugon ang ibinigay ng nawawalang path, tiyaking nakarehistro ang 404 handler. Kung 404 ang /private/report, tingnan ang method at path pattern.
Tukuyin ang uri ng failure
Itinatala ng Vert.x Web ang mga route-match failure na ito:
| Status | Match failure |
|---|---|
| 404 | Walang tumugmang path |
| 405 | Tumugma ang path, hindi ang HTTP method |
| 406 | Walang response media type na tugma sa Accept |
| 415 | Hindi ma-consume ang request Content-Type |
| 400 | Hindi tumatanggap ng walang-lamang body ang tumugmang route |
Gamitin ang Router.errorHandler(statusCode, handler) para magtakda ng tugon sa mga status na ito. Samantala, pinangangasiwaan ng Route.failureHandler ang request na pumasok sa failure flow dahil tumawag ang tumugmang handler sa ctx.fail(...) o nag-throw ng exception, ayon sa matching criteria ng route. Kapag tinapos na ng normal handler ang tugon, hindi na nito ipinapasa ang control sa susunod na failure handler.
Huwag tawagin ang RoutingContext.next() sa loob ng Router.errorHandler; ayon sa API docs, dapat nitong tapusin ang error response sa halip na ituloy ang normal na failure routing.
Opisyal na sanggunian
[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)