全局异常处理程序从未在microaut 3项目反应堆中被调用



在microaut应用程序从2.5.12升级到3.0.0并使用项目反应器作为反应流之后。全局异常处理程序方法永远不会被调用。

public class GlobalException extends RuntimeException{
public GlobalException(Throwable throwable){super(throwable);}
}
@Produces
@Singleton
@Requires(classes = {GlobalException.class, ExceptionHandler.class})
public class GlobalExceptionHandler implements ExceptionHandler<GlobalException, HttpResponse> {
private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@Override
public HttpResponse handle(HttpRequest request, GlobalException exception) {
LOG.error(exception.getLocalizedMessage());
LOG.error(exception.getCause().getMessage());
Arrays.stream(exception.getStackTrace()).forEach(item -> LOG.error(item.toString()));
return HttpResponse.serverError(exception.getLocalizedMessage());
}
}

对于下面代码的异常,处理程序方法永远不会被调用

@Override
public Flux<FindProductCommand> get(ProductSearchCriteriaCommand searchCriteria) {
LOG.info("Controller --> Finding all the products");
return iProductManager.find(searchCriteria).onErrorMap(throwable -> {
return new GlobalException(throwable);
});
}

我在rxjava 3的microaut Java中有这个代码全局异常处理,它工作得很好,但是,现在有了项目反应堆,它不工作了

如果您只打算生成单个元素,则应该使用@SingleResultOr use Mono注释

@Override
@SingleResult
public Flux<List<FindProductCommand>> freeTextSearch(String text) {
LOG.info("Controller --> Finding all the products");
return iProductManager.findFreeText(text)
.onErrorMap(throwable -> {
throw new GlobalException(throwable);
});
}

试试这个:

@Override
public Flux<FindProductCommand> get(ProductSearchCriteriaCommand searchCriteria) {
LOG.info("Controller --> Finding all the products");
return iProductManager
.find(searchCriteria)
.onErrorResume(e -> Mono.error(new GlobalException("My exception", e));
}

这样做的一个原因可能是,在你的代码中,看看这一点:

@Override
public Flux<FindProductCommand> get(ProductSearchCriteriaCommand searchCriteria) {
LOG.info("Controller --> Finding all the products");
return iProductManager.find(searchCriteria).onErrorMap(throwable -> {
return new GlobalException(throwable);
});
}

如果代码iProductManager.find(searchCriteria)调用rest端点并获得404未找到,您将不会在错误处理程序中获得错误。相反,结果是您将得到相当于Optional.empty()的值。

如果你想强制执行错误,你可以把它改成:

iProductManager.find(searchCriteria).switchIfEmpty(Mono.error(...))

最新更新