4

我试图了解这是一个功能还是一个错误...... :-)

对于下面的控制器和异常映射器,对于将失败并返回 401 响应的 rest 客户端,我希望在这两种情况下都调用异常处理程序。但是,它不会为 WebApplicationException 调用。为什么会这样,以及您打算如何为它们注册异常处理程序。这是使用 Quarkus 版本 0.21.2

@Path("/failable")
public class FailableResource {

  @Inject
  @RestClient
  private SomeHttpClient httpClient;

  @GET
  @Path("fails")
  @Produces(MediaType.TEXT_PLAIN)
  public String fails() {
    try {
      return httpClient.someQuery();
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }

  @GET
  @Path("works")
  @Produces(MediaType.TEXT_PLAIN)
  public String works() {
    try {
      return httpClient.someQuery();
    } catch (Exception e) {
      e.printStackTrace();
      throw new IllegalStateException("Not a WebApplicationException");
    }
  }
}

和异常映射器

@Provider
public class HandleMySillyError implements ExceptionMapper<Throwable> {

  @Override
  public Response toResponse(Throwable e) {
    e.printStackTrace();
    return Response.ok("Some handled response").build();
  }
}
4

1 回答 1

1

I found out when running in quarkus:dev mode the exception mapper is not invoked. It seems that this is caused by an exception mapper from quarkus that is only used in DEV mode (see https://github.com/quarkusio/quarkus/issues/7883).

I launched my code local as normal a normal java program, causing my exception handler to work as expected. Also when running the code on Openshift, my custom exception mapper is used as well.

note: I used quarkus version 1.8.3

于 2020-10-24T19:31:21.800 回答