Rxjs吞下错误

Rxjs swallows errors

本文关键字:错误 Rxjs      更新时间:2023-09-26

有一个简单的Rxjs流,我面临这样的情况:

Rx.Observable
  .fromArray([1,2,3,4,5,6])
// if commented from here 
  .windowWithCount(2, 1)
  .selectMany(function(x) {
    return x.toArray();
  })
// to here .. the error bubbles up
  .subscribe(function(x) {
    console.log('x:',x)
    throw new Error("AAHAAHHAHA!");  
  });

使用windowWithCount + selectMany,错误在内部被静默捕获,并且不可捕获,并且也不会在控制台中通知

注释这两个块,至少在控制台通知错误
我觉得这是不应该的,还是我错过了什么?
这里jsbin

订阅函数不应该抛出异常。RxJs建模异步信息流,其中观察者代码通常与生产者代码异步运行(例如不在同一个调用堆栈上)。您不能依赖于错误向后传播到生产者。

Rx.Observable
  .fromArray([1,2,3,4,5,6])
// if commented from here 
  .windowWithCount(2, 1)
  .selectMany(function(x) {
    return x.toArray();
  })
// to here .. the error bubbles up
  .subscribe(function(x) {
    try {
      console.log('x:',x)
      throw new Error("AAHAAHHAHA!");
    }
    catch (e) { console.log('error: ' + e); }
  });

话虽这么说,看起来RxJS正在"吃掉"这个特殊的异常,这可能是一个bug。RxJS尽最大努力将未观察到的异常作为主机中未处理的异常引发。看起来在这个例子中,这个机制不起作用。你应该在GitHub上打开一个issue