24 Jul 2024
2 min

catchError – RxJS Reference

catchError

Just as a plain observable catchError passes values along to the observer until an error in the source stream occurs. When that happens, the operator executes a callback function passing in the error. The function is expected to return an observable that will replace the original source observable.

This replacement observable is then going to be subscribed to and its values are going to be used in place of the errored out input observable. If an error occurs in the replacement observable, the operator will send the error notification to the observer.

Since the callback function takes the source observable that produced the error as an argument alongside the error, it is possible to retry that observable by returning the original observable again.

The operator works in the following way:

  1. Subscribe to a source observable
  2. When a new value arrives from a source observable, send it to the observer
  3. If the source observable throws an error, execute a callback function and subscribe to the returned replacement observable
  4. When a new value arrives from the replacement observable, send it to the observer
  5. Once the replacement observable completes, send the complete notification to the observer
  6. If the replacement observable throws an error, send the error notification to the observer

Usage

Errors in programs happen all the time and RxJS is no exception. For example, a network request can fail because of numerous reasons. Since you can pass an error handler as part of the observer when subscribing to the observable, that’s a one way to handle an error gracefully. And sometimes that’s all we need, but this error handling approach is limited.

Using error handling inside an observer, we cannot, for example, recover from the error or emit an alternative fallback value that replaces the value that we were expecting from the backend. That’s where catchError operator comes in handy.

Here’s an example of using catchError to try a different URL if the original fails:

const server1 = 'http://url-to-fail.com';
const server2 = 'https://api.mocki.io/v1/b043df5a';

fromFetch(server1).pipe(
   catchError((err) => fromFetch(server2))
).subscribe((res) => res.status);

Playground

Additional resources

See also

Frequently asked questions

How do I use catchError to fall back to another observable in RxJS?

Use the catchError operator in a pipe() and return a replacement observable from its callback. The callback receives the error, and the returned observable is subscribed to in place of the failed source, so its values reach the observer. In the article's example, fromFetch(server1) falls back to fromFetch(server2) when the first request fails, so the subscriber still receives a response with a status property.

What is the difference between catchError and the error callback in subscribe()?

The error callback in subscribe() only lets you handle the error, while catchError can recover from it by returning a replacement observable or a fallback value. Error handling inside an observer cannot replace the value you were expecting from the backend. Use catchError in the pipe() when the stream should keep emitting values after a failure, and keep the observer's error handler for final logging or display.

How do I retry a failed Observable with catchError in RxJS?

Return the source observable again from the catchError callback, since the callback receives the source observable that produced the error alongside the error. Subscribing to it again performs the retry. Related operators retry and retryWhen cover this use case as well, so compare them when you need configurable retry logic.

What happens if the fallback observable in catchError also throws an error?

If the replacement observable returned by the catchError callback throws an error, the operator sends that error notification straight to the observer. The original error is not recovered from in that case. To handle a failing fallback, chain another catchError after the first one in the pipe(), so the second error is caught there.

When does catchError send the complete notification to the observer?

The stream completes when the replacement observable completes, because catchError sends the complete notification to the observer at that point. Values from the replacement are forwarded as they arrive, so the subscriber never sees the original error as a notification. Only an error from the replacement itself is passed on to the observer as an error.

Share this post

Sign up for our newsletter

Stay up-to-date with the trends and be a part of a thriving community.