Vertx - 使用 Router failureHandler 处理异步调用中的错误 [英] Vertx - using the Router failureHandler for the errors in async calls

查看:40
本文介绍了Vertx - 使用 Router failureHandler 处理异步调用中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们最近在 Vertx 路由器中发现了 failureHandler>

我们认为它可以帮助我们摆脱所有重复的 try-catch 块.但可惜的是,回调中抛出的异常似乎没有被 failureHandler 捕获.

示例:下面,failureHandler 仅在第 3 种情况下被调用:

//获取用户详细信息router.get("/user").handler(ctx -> {ctx.response().headers().add("content-type", "application/json");//一些异步操作userApiImpl.getUser(ctx, httpClient, asyncResult -> {//ctx.response().setStatusCode(404).end();//1//throw new RuntimeException("某事发生");//2ctx.fail(404);//3});});//============================//错误处理程序//============================router.get("/user").failureHandler(ctx -> {LOG.info("错误处理程序正在执行中.");ctx.response().setStatusCode(ctx.statusCode()).end("方法出错");});

  1. 这是否符合预期?
  2. 我们能否以某种方式在路由器中为异步上下文中发生的异常声明一个全局的 try-catch?

解决方案

预计手动发送带有错误代码的响应不会触发失败处理程序.

应该在以下情况下触发:

  1. 路由路径匹配

  2. 处理程序抛出异常或调用 ctx.fail()

这是一个例子:

Route route1 = router.get("/somepath/path1/");route1.handler(routingContext -> {//假设这会抛出一个 RuntimeExceptionthrow new RuntimeException("出事了!");});Route route2 = router.get("/somepath/path2");route2.handler(routingContext -> {//这个故意让传入状态码的请求失败//例如403 - 禁止routingContext.fail(403);});//定义一个失败处理程序//这将在上述处理程序中的任何失败时被调用Route route3 = router.get("/somepath/*");route3.failureHandler(failureRoutingContext -> {int statusCode = failureRoutingContext.statusCode();//RuntimeException 的状态码为 500,其他失败的状态码为 403HttpServerResponse 响应 = failureRoutingContext.response();response.setStatusCode(statusCode).end("对不起!不是今天");});

请参阅 Vert.x Web 文档的错误处理部分

We have recently discovered the failureHandler in the Vertx router

We thought it could help us get rid of all the repetitive try-catch blocks we have. But alas, it seems that the exceptions that are thrown inside the callbacks are not caught by the failureHandler.

Example: below, the failureHandler is called only for the 3rd case:

// 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");
        });

  1. Is this as expected?
  2. Can we somehow declare a global try-catch in a router for the exceptions occurring in the async context?

解决方案

It is expected that sending a response manually with an error code does not trigger the failure handler.

It should be triggered if:

  1. the route path matches

  2. a handler throws an exception or ctx.fail() is invoked

Here's an example:

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");

});

See the error handling section of the Vert.x Web documentation

这篇关于Vertx - 使用 Router failureHandler 处理异步调用中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆