RxJS Error Handling

Tram Ho

Reactive Programming uses the concept of stream to interact with data. Ideally, the stream always returns data and there is no error, then the stream may still exist or complete() and end. However, if an error occurs, the stream will automatically end, then the user may not be able to interact with the application and must restart all without retry .

Here are some possible ways to fix this problem.


1. catchError ().

catchError () will be called when the stream has an error and always return a new Observable. The main idea here is that when one stream fails, use another stream.

When stream$ error occurs, it will call operator catchError () and return newStream$ . Now stream$ has stopped working and is replaced by newStream$ .

2. onErrorResumeNext ().

Similar to catchError() , losing one glue shows another, exchanging streams!

3. retry () / retryWhen ().

Once failed, sure nothing 2 times 3 did not. Handsome without face, try again and again.


The above are some ways that can be applied when the stream fails. Reference source:

Share the news now

Source : Viblo