I have a list of items to parse, but the parsing of one of them can fail.
What is the \"Rx-Way\" to catch error but continue executing the sequence
Code Samp
You need to switch to a new disposable stream, and if an error occurs within it will be disposed safely, and keep the original stream alive:
Rx.Observable.from([0,1,2,3,4,5])
.switchMap(value => {
// This is the disposable stream!
// Errors can safely occur in here without killing the original stream
return Rx.Observable.of(value)
.map(value => {
if (value === 3) {
throw new Error('Value cannot be 3');
}
return value;
})
.catch(error => {
// You can do some fancy stuff here with errors if you like
// Below we are just returning the error object to the outer stream
return Rx.Observable.of(error);
});
})
.map(value => {
if (value instanceof Error) {
// Maybe do some error handling here
return `Error: ${value.message}`;
}
return value;
})
.subscribe(
(x => console.log('Success', x)),
(x => console.log('Error', x)),
(() => console.log('Complete'))
);
More info on this technique in this blog post: The Quest for Meatballs: Continue RxJS Streams When Errors Occur