Vertx - 使用 Router failureHandler 处理异步调用中的错误
Vertx - using the Router failureHandler for the errors in async calls
我们最近在 Vertx 路由器中发现了 failureHandler
我们认为它可以帮助我们摆脱所有重复的 try-catch
障碍。但是,唉,似乎回调中抛出的异常没有被 failureHandler
捕获。
示例:下面,仅针对第三种情况调用 failureHandler
:
// Get the user details
router.get("/user").handler(ctx -> {
ctx.response().headers().add("content-type", "application/json");
// some async operation
userApiImpl.getUser(ctx, httpClient, asyncResult -> {
// ctx.response().setStatusCode(404).end(); //1
// throw new RuntimeException("sth happened"); //2
ctx.fail(404); //3
});
});
// ============================
// ERROR HANDLER
// ============================
router.get("/user").failureHandler(ctx -> {
LOG.info("Error handler is in the action.");
ctx.response().setStatusCode(ctx.statusCode()).end("Error occurred in method");
});
- 这是预期的吗?
- 我们能否以某种方式在路由器中为异步上下文中发生的异常声明全局
try-catch
?
预计手动发送带有错误代码的响应不会触发故障处理程序。
在以下情况下应触发:
路由路径匹配
处理程序抛出异常或调用 ctx.fail()
这是一个例子:
Route route1 = router.get("/somepath/path1/");
route1.handler(routingContext -> {
// Let's say this throws a RuntimeException
throw new RuntimeException("something happened!");
});
Route route2 = router.get("/somepath/path2");
route2.handler(routingContext -> {
// This one deliberately fails the request passing in the status code
// E.g. 403 - Forbidden
routingContext.fail(403);
});
// Define a failure handler
// This will get called for any failures in the above handlers
Route route3 = router.get("/somepath/*");
route3.failureHandler(failureRoutingContext -> {
int statusCode = failureRoutingContext.statusCode();
// Status code will be 500 for the RuntimeException or 403 for the other failure
HttpServerResponse response = failureRoutingContext.response();
response.setStatusCode(statusCode).end("Sorry! Not today");
});
请参阅 Vert.x 网络文档的 error handling 部分
我们最近在 Vertx 路由器中发现了 failureHandler
我们认为它可以帮助我们摆脱所有重复的 try-catch
障碍。但是,唉,似乎回调中抛出的异常没有被 failureHandler
捕获。
示例:下面,仅针对第三种情况调用 failureHandler
:
// Get the user details
router.get("/user").handler(ctx -> {
ctx.response().headers().add("content-type", "application/json");
// some async operation
userApiImpl.getUser(ctx, httpClient, asyncResult -> {
// ctx.response().setStatusCode(404).end(); //1
// throw new RuntimeException("sth happened"); //2
ctx.fail(404); //3
});
});
// ============================
// ERROR HANDLER
// ============================
router.get("/user").failureHandler(ctx -> {
LOG.info("Error handler is in the action.");
ctx.response().setStatusCode(ctx.statusCode()).end("Error occurred in method");
});
- 这是预期的吗?
- 我们能否以某种方式在路由器中为异步上下文中发生的异常声明全局
try-catch
?
预计手动发送带有错误代码的响应不会触发故障处理程序。
在以下情况下应触发:
路由路径匹配
处理程序抛出异常或调用 ctx.fail()
这是一个例子:
Route route1 = router.get("/somepath/path1/");
route1.handler(routingContext -> {
// Let's say this throws a RuntimeException
throw new RuntimeException("something happened!");
});
Route route2 = router.get("/somepath/path2");
route2.handler(routingContext -> {
// This one deliberately fails the request passing in the status code
// E.g. 403 - Forbidden
routingContext.fail(403);
});
// Define a failure handler
// This will get called for any failures in the above handlers
Route route3 = router.get("/somepath/*");
route3.failureHandler(failureRoutingContext -> {
int statusCode = failureRoutingContext.statusCode();
// Status code will be 500 for the RuntimeException or 403 for the other failure
HttpServerResponse response = failureRoutingContext.response();
response.setStatusCode(statusCode).end("Sorry! Not today");
});
请参阅 Vert.x 网络文档的 error handling 部分