Exploring error handling patterns and strategies
The first classic pattern we will learn for handling errors is based on the subscribe()
method. The subscribe()
method takes as input the object Observer, which has three callbacks:
- A success callback: This is called every time the stream emits a value and receives as input the value emitted.
- An error callback: This is called when an error occurs and receives as input the error itself.
- A completion callback: This is called when the stream completes.
So, in order to handle the error, the first possibility is implementing the error callback. In the following code sample, stream$
represents our Observable, and we implemented the following:
- A success callback that logs the received value in the console
- An error callback that logs the received error in the console
- A complete callback that logs the stream completion:
stream$.subscribe({ &...