BACK
ReferencecatchError
Reference · 32

catchError

catchError

rxjs.dev

Catches an error and returns a fallback Observable.

Signature

catchError(selector: (err: any, caught: Observable) => ObservableInput): OperatorFunction

Marble diagram

in:           --1--2--#
catch->fallback: --1--2--f|

What it does

On a source error it calls selector(err, caught). The returned Observable becomes the stream's continuation. The second argument, caught, is the original Observable; re-subscribing to it implements a "retry by hand".

When to use

A fallback value (catchError(() => of([]))), transforming errors (throwError(() => new MyError(...))), a silencer (EMPTY).

Gotcha

catchError replaces the stream. After it, the following operators work with the new stream, and the original no longer emits.

See also

retry, finalize, throwError

script.ts // TypeScript
CONSOLE · Console output
Hit Run to see the result...