Observable stops firing even when catching the error

TomerMiz

I'm facing with a very strange behavior on my project, I have a simple Angular service with the below code:

seatClick$ = new Subject<Seat>();

and a method on the service that fires the observable:

  handleSeatClick(seat: Seat) {
    this.seatClick$.next(seat);
  }

the observable logic is simple:

this.seatClick$.pipe(
    exhaustMap((seat: Seat) => {
       this.someFunctionThatThrowsException(); // this function throws ref exception
      return of(null);
    })
    , catchError(err => {
        console.log('error handled');
        return of(null);
    })
)
.subscribe(() => {
    console.log('ok');
  },
  (error1 => {
    console.log('oops');
  })
);

This is really strange, when "someFunctionThatThrowsException" is called it throws some ReferenceError exception, this exception is then catched with the catchError and the next() event is fired.

However, from this moment on the seatClick observable stops responding, as if it was completed, calling handleSeatClick on the service won't respond any more.

What am I missing here?

satanTime

That's correct behavior, you need repeat operator here to resubscribe.

this.seatClick$.pipe(
    exhaustMap((seat: Seat) => {
       this.someFunctionThatThrowsException();
       return of(null);
    })

    // in case of an error the stream has been completed.
    , catchError(err => {
        console.log('error handled');
        return of(null);
    })

    // now we need to resubscribe again
    , repeat() // <- here
)
.subscribe(() => {
    console.log('ok');
  },
  (error1 => {
    console.log('oops');
  })
);

also if you know that something might fail you can dedicate it to an internal stream and use catchError there, then you don't need repeat.

this.seatClick$.pipe(
  // or exhaustMap, or mergeMap or any other stream changer.
  switchMap(seal => of(seal).pipe(
    exhaustMap((seat: Seat) => {
       this.someFunctionThatThrowsException();
       return of(null);
    })
    , catchError(err => {
        console.log('error handled');
        return of(null);
    })
  )),
  // now switchMap always succeeds with null despite an error happened inside
  // therefore we don't need `repeat` outside and our main stream
  // will be completed only when this.seatClick$ completes
  // or throws an error.
)
.subscribe(() => {
    console.log('ok');
  },
  (error1 => {
    console.log('oops');
  })
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Error callback always firing even when successful on the server

How to handle `UnhandledPromiseRejectionWarning` when using Observable.from(<Promise>) and catching error in the Observable

Observable stops emitting when nested error handler calls onExceptionResumeNext

Observable continues to retry subscription after catching error

ngOnChanges not firing when attribute changed by Observable/subscrption

firing custom extender of one observable when another computed observable is changed

When new Reference is added, excel stops firing events

DataMember Error when firing CellValueChanged

Why does fetch return even after catching an error?

fetching from a URL shows an error in console even after catching it?

Redirecting when catching an error from axios request

Catching PHP Parser error when using include

Get content text when catching error in RxJs

React/Redux/Reselect - mapStateToProps stops firing even after seeing changes reflected in the reducer.

Subscribe on observable not firing when using an async extension method

useEffect firing even when dependency array is not true or has not changed

Google Analytics event firing even when there are validation errors

call to DynamoDB from a lambda stops dead, not even an error

While loop within function stops even when conditions not met

Chrome Dev Tools stops at breakpoint even when breakpoints are deactivated

Why does the while loop in JS stops even when condition is met?

Application_Error not firing when customerrors = "On"

Script stops execution when faces an error

Broken pipe error when reading stops

Observable forkJoin not firing

Observable success method is not firing

Observable not firing in angular ngOnInit

Observable Subscription Not Firing

Catching error in a shell script when performing git checkout