Rxjs swallows errors

1.2k views Asked by At

Having a simple Rxjs stream, i faced this situation:

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

with the windowWithCount + selectMany the error is silently catched internally and isn't catchable and it isn't notified in console neither

commenting those 2 blocks the error is notified on console at least
I don't think this is supposed to be, or am i missing something?
here the jsbin

1

There are 1 answers

3
Brandon On BEST ANSWER

Your subscribe function should never throw an exception. RxJs models asynchronous streams of information, where the observer code often runs asynchronously from the producer code (e.g. not on the same callstack). You cannot depend upon errors propagating back up to the producer.

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

That being said, it looks like RxJS is "eating" this particular exception, which may be a bug. RxJS does its best to raise unobserved exceptions as unhandled exceptions in the host. Looks like in this instance this mechanism isn't working. You should open an issue on GitHub.