Catch and continue

Sooner or later, we will have a stream that will throw an error. Let's see what that can look like:

// example of a stream with an error

let stream$ = Rx.Observable.create(observer => {
observer.next(1);
observer.error('an error is thrown');
observer.next(2);
});

stream$.subscribe(
data => console.log(data), // 1
error => console.error(error) // 'error is thrown'
);

In the preceding code, we set up a scenario where we first emit a value, followed by emitting an error. The first value is captured in our first callback in our subscribe method. The second emitted thing, the error, is captured by our error callback. The third emitted value does not get emitted to our subscriber because our stream has been interrupted by the error. There is something we can do here, and that is to use the catch() operator. Let's apply that to our stream and see what happens:

// error-handling/error-catch.js
const Rx = require("rxjs/Rx");

let stream$ = Rx.Observable.create(observer => {
observer.next(1);
observer.error("an error is thrown");
observer.next(2);
}).catch(err => Rx.Observable.of(err));

stream$.subscribe(
data => console.log(data), // emits 1 and 'error is thrown'
error => console.error(error)
);

Here, we capture our error with the catch() operator. In the catch() operator, we take our error and emit it as a normal Observable using the of() operator. What happens to the 2 we emit, though? Still no luck with that one. The catch() operator is able to take our error and turn it into a normal emitted value; instead of an error, we don't get all the values from the stream. 

Let's have a look at a scenario when we are dealing with multiple streams:

// example of merging several streams

let merged$ = Rx.Observable.merge(
Rx.Observable.of(1),
Rx.Observable.throw("err"),
Rx.Observable.of(2)
);

merged$.subscribe(data => console.log("merged", data));

In the scenario above, we merge three streams. The first stream emits the number 1 and nothing else gets emitted. This is due to our second stream tearing everything down, as it emits an error. Let's try to apply our newfound catch() operator and see what happens:

// error-handling/error-merge-catch.js

const Rx = require("rxjs/Rx");

let merged$ = Rx.Observable.merge(
Rx.Observable.of(1),
Rx.Observable.throw("err").catch(err => Rx.Observable.of(err)),
Rx.Observable.of(2)
);

merged$.subscribe(data => console.log("merged", data));

We run the above code and we notice that the 1 is emitted, the error is emitted as a normal value, and, finally, even the 2 is emitted. Our conclusion here is that it is a good idea to apply a catch() operator to a stream before it is being merged with our streams.

As before, we can also conclude that the catch() operator is able to stop the stream from just erroring out, but that other values that would have been emitted after the error are effectively lost. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.115.16