softshipper
softshipper

Reputation: 34099

Hot observable error handling

I have an observable, that listens for the keyup event from an `input.

On every keyup, it makes a request to the server and if there aren't any values, it will throw an exception.

.map(function (oResp) {
  if (oResp.data.results.length === 0) {
    throw new Error(self.getTextBundle("insPlantInvalid"));
  } 
  return oResp.data.results;
})

After the exception was thrown, the observable is not going to listen for the event anymore.

Does the exception handling in rxjs work in this way?

Upvotes: 0

Views: 326

Answers (2)

Gaurav Mukherjee
Gaurav Mukherjee

Reputation: 6335

.catch() can also be used to handle error.

this.http.get('url').map(res => res.json()).catch(() => Observable.of({error: true})

This is return {error: true} in case of error and the event observable wont break. But in the map operator which comes next to this response needs to handle this case.

Upvotes: 0

martin
martin

Reputation: 96949

Yes, this is correct behavior. Exception makes the operator send an error notification. Both error or complete signals have to be the last signals emitted and both also cause unsubscription.

To resubscribe or handle errors there are operators such as retry(), retryWhen(), catch() or onErrorResumeNext(). I guess you already know these.

Upvotes: 1

Related Questions