没有在订阅中捕获Observable.map引发的错误

时间:2018-10-24 19:23:29

标签: java rx-java project-reactor

代码如下:

public void initiateProcess() {

findAbandonedOpenOrders() //returns Observable<AsyncN1QueryResult>
    .flatMap(results -> results.rows())
    .map(row -> row.value())
    .map(s -> processStringToGetOrderId(s.toString()))
    .map(
        o -> {
          log.info("Generating access token for orderId: {}", o);
          return identityConnector
              .getServiceTokenFromIdentity()
              .map(
                  issueToken ->
                      RequestInputModel.builder()
                          .authorisationToken(issueToken.getAccessToken())
                          .orderId(o)
                          .build())
              .map(
                  requestInputModel -> {
                    log.info(
                        "Invoking cancel order for orderId: {}",
                        requestInputModel.getOrderId());
                    return cancelOrderApiConnector
                        .invokeAPI(
                            requestInputModel,
                            RequestInputModel.RequestBodyModel.builder().build())  //throw RuntimeException as soon as the flow enters this method
                        .subscribe();
                  })
              .subscribe();
        })
    .subscribe(
        s -> {},
        e -> {
          log.error(ExceptionUtils.getStackTrace(e));
        });
  }

一旦我调用cancelOrderApiConnector.invokeAPI,它将引发运行时异常。因此,立即映射应该抛出它,而后者又应该由最外面的映射抛出。但这没有发生。堆栈跟踪不会被打印(逻辑在订阅中实现)。

有人可以告诉我我可能做错了吗?

1 个答案:

答案 0 :(得分:1)

不要破坏反应链,将这些map(v -> codeThatProduceAFlux.subscribe())替换为flatMap(v -> codeThatProduceAFlux),错误将在子步骤之间正确传播