Handling Error
The fact is if there is any of your stream breakdown with an error, the whole system can go wrong. Here is a simple example of how your system can go down hopelessly without any expectation:
That why handling error is so important in RxJS. Sadly, I haven't found any global way to declare error handler in React Epic. So in a long run, we have to handle error manually:
But handling errors the wrong way can be troublesome:
In the above example, everything is fine. Except, the stream will be suspended and cannot resume if any exception is thrown at runtime. Here is the reason why. As usual you will type:
without understanding how it works. By that way, you replace the current stream with a stream of the error object you handle. That's why the stream will be freezed after you handle the first error.
So here is a simple work around on how to handle the error in RxJS:
But using this way, we can not reset the state of the stream if we plan to use scan
or throttle
. So the way to deal with catchError
without interfere the source stream is to lift the stream into nested stream using mergeMap
:
However, the first case is still clean if we don't plan to store any state in the stream but still keep the stream flat.
For a conclusion, I will say that even error handler can be failed and the line of handling error or is quite blurry sometimes. So make sure that you added an error handler and not overthinking how to handle the error!
Last updated
Was this helpful?