Vert.x Web: เลือก failureHandler หรือ errorHandler สำหรับ 403, 404 และ 500
ใน Vert.x Web ให้แยกความผิดพลาดของ handler บน route ที่ตรงกับคำขอออกจากคำขอที่ไม่ตรงกับ route ใดเลย ใช้ Route.failureHandler กับความผิดพลาดที่จำกัดตาม path และ method; ใช้ Router.errorHandler กับการตอบตาม status เช่น 404 สำหรับ path ที่ไม่พบ ตัวอย่าง Java นี้อ้างอิงเอกสาร Vert.x Web 5.2.3 ที่ตรวจเมื่อ 20-11-2026
ใน Vert.x Web ให้แยกความผิดพลาดของ handler บน route ที่ตรงกับคำขอออกจากคำขอที่ไม่ตรงกับ route ใดเลย ใช้ Route.failureHandler กับความผิดพลาดที่จำกัดตาม path และ method; ใช้ Router.errorHandler กับการตอบตาม status เช่น 404 สำหรับ path ที่ไม่พบ ตัวอย่าง Java นี้อ้างอิงเอกสาร Vert.x Web 5.2.3 ที่ตรวจเมื่อ 20-11-2026
สิ่งที่ต้องมี
เพิ่ม io.vertx:vertx-web ด้วยเวอร์ชันตาม Vert.x BOM หรือนโยบายของโปรเจกต์ เอกสารทางการ 5.2.3 แสดง Maven dependency นี้:
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>5.2.3</version>
</dependency>ลงทะเบียน route และ handler ตามสถานะ
สร้าง router ผูก failure handler กับ route แล้วลงทะเบียนการตอบกลับตาม 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);route นี้ตรงกับคำขอ GET ใต้ /private/* ctx.fail(403) เริ่มกระบวนการ failure; failure handler ของ route เดียวกันอ่าน status และจบ response หาก handler ที่ตรงกันโยน exception ก็อาจเกิด failure 500 ได้ จึงควรส่ง body ที่ปลอดภัยตาม status จริง
Router.errorHandler(404, ...) จัดการกรณีไม่มี path ที่ตรงกัน ส่วน Router.errorHandler(500, ...) เป็น response สำรองตามสถานะได้ เมื่อ failure handler อื่นไม่ได้เขียน response หรือมี exception ที่ยังไม่ถูกจัดการ อย่าเปิดเผยข้อความ exception หรือรายละเอียดภายในให้ client
ตรวจสอบ response
หลังเริ่ม server ให้ส่งคำขอจาก terminal อีกหน้าต่าง:
curl -i http://localhost:8080/private/report
curl -i http://localhost:8080/does-not-existผลที่คาดคือ status 403 พร้อม body forbidden แล้ว status 404 พร้อม not_found หาก path ที่ไม่มีส่งค่าเริ่มต้นกลับมา ให้ตรวจว่าลงทะเบียน 404 handler แล้ว หาก /private/report ได้ 404 ให้ตรวจ method และรูปแบบ path
แยกประเภท failure
Vert.x Web ระบุ route-match failure ดังนี้:
| Status | ความผิดพลาดในการจับคู่ |
|---|---|
| 404 | ไม่มี path ที่ตรงกัน |
| 405 | path ตรงกัน แต่ HTTP method ไม่ตรง |
| 406 | ไม่มี response media type ที่ตรงกับ Accept |
| 415 | ไม่สามารถรับ Content-Type ของ request |
| 400 | route ที่ตรงกันไม่รับ body ว่าง |
กำหนด response สำหรับ status เหล่านี้ด้วย Router.errorHandler(statusCode, handler) ส่วน Route.failureHandler ใช้จัดการ request ที่เข้าสู่ failure flow เพราะ handler ที่ตรงกันเรียก ctx.fail(...) หรือโยน exception โดยพิจารณาเงื่อนไขของ route หาก handler ปกติจบ response ไปแล้ว ก็จะไม่ส่ง control ต่อให้ failure handler ถัดไป
อย่าเรียก RoutingContext.next() ภายใน Router.errorHandler; เอกสาร API ระบุว่า handler นี้ควรจบ error response แทนการทำ failure routing ปกติต่อ
แหล่งข้อมูลทางการ
[เอกสาร Java Vert.x Web 5.2.3](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)