Can't catch the error from BehaviorSubject, or any subject in rxjs 7

41 Views Asked by At

The idea is to catch the error message Next err =================> thrown after 6 sec. In console log we can see only first thrown error, which is werry strange behavior.
Here is link in stakblitz. https://stackblitz.com/edit/typescript-tdpwu3?file=index.ts

import {
  interval,
  of,
  throwError,
  timer,
  ReplaySubject,
  Subject,
  BehaviorSubject,
} from 'rxjs';
import { mergeMap, retry, tap } from 'rxjs/operators';

//emit value every 1s
let source = new Subject();

const example = source.pipe(
  tap((ob) => console.log('OBJ: ', ob)),
  //retry 2 times on error
  retry({
    delay: (errors) => {
      console.log('Last error : ', errors);
      return timer(3000);
    },
  })
);

setTimeout(() => {
  source.error({ message: 'Error here !!!' });
}, 3000);

setTimeout(() => {
  console.log('Next err =================>');
  //source = new BehaviorSubject({ status: 'open' });
  source.error({ message: 'Next err =================>' });
}, 6000);

const subscribe = example.subscribe();

source.next({ status: 'open' });
1

There are 1 best solutions below

0
On

I don't see strange behavior here. The source observable is closed once it emits the first error { message: 'Error here !!!' }.

From the docs:

If either an Error or Complete notification is delivered, then nothing else can be delivered afterwards.

The second error { message: 'Next err =================>' } is never emitted to the observable. So the second error is never seen inside the retry.