Debian Codegraph @

node/rxjs/src/internal/operators/catchError.ts

raw
1 import { Observable } from '../Observable';
2
3 import { ObservableInput, OperatorFunction, ObservedValueOf } from '../types';
4 import { Subscription } from '../Subscription';
5 import { innerFrom } from '../observable/innerFrom';
6 import { createOperatorSubscriber } from './OperatorSubscriber';
7 import { operate } from '../util/lift';
8
9 export function catchError<T, O extends ObservableInput<any>>(
10 selector: (err: any, caught: Observable<T>) => O
11 ): OperatorFunction<T, T | ObservedValueOf<O>>;
12
13 /**
14 * Catches errors on the observable to be handled by returning a new observable or throwing an error.
15 *
16 * <span class="informal">
17 * It only listens to the error channel and ignores notifications.
18 * Handles errors from the source observable, and maps them to a new observable.
19 * The error may also be rethrown, or a new error can be thrown to emit an error from the result.
20 * </span>
21 *
22 * ![](catch.png)
23 *
24 * This operator handles errors, but forwards along all other events to the resulting observable.
25 * If the source observable terminates with an error, it will map that error to a new observable,
26 * subscribe to it, and forward all of its events to the resulting observable.
27 *
28 * ## Examples
29 *
30 * Continue with a different Observable when there's an error
31 *
32 * ```ts
33 * import { of, map, catchError } from 'rxjs';
34 *
35 * of(1, 2, 3, 4, 5)
36 * .pipe(
37 * map(n => {
38 * if (n === 4) {
39 * throw 'four!';
40 * }
41 * return n;
42 * }),
43 * catchError(err => of('I', 'II', 'III', 'IV', 'V'))
44 * )
45 * .subscribe(x => console.log(x));
46 * // 1, 2, 3, I, II, III, IV, V
47 * ```
48 *
49 * Retry the caught source Observable again in case of error, similar to `retry()` operator
50 *
51 * ```ts
52 * import { of, map, catchError, take } from 'rxjs';
53 *
54 * of(1, 2, 3, 4, 5)
55 * .pipe(
56 * map(n => {
57 * if (n === 4) {
58 * throw 'four!';
59 * }
60 * return n;
61 * }),
62 * catchError((err, caught) => caught),
63 * take(30)
64 * )
65 * .subscribe(x => console.log(x));
66 * // 1, 2, 3, 1, 2, 3, ...
67 * ```
68 *
69 * Throw a new error when the source Observable throws an error
70 *
71 * ```ts
72 * import { of, map, catchError } from 'rxjs';
73 *
74 * of(1, 2, 3, 4, 5)
75 * .pipe(
76 * map(n => {
77 * if (n === 4) {
78 * throw 'four!';
79 * }
80 * return n;
81 * }),
82 * catchError(err => {
83 * throw 'error in source. Details: ' + err;
84 * })
85 * )
86 * .subscribe({
87 * next: x => console.log(x),
88 * error: err => console.log(err)
89 * });
90 * // 1, 2, 3, error in source. Details: four!
91 * ```
92 *
93 * @see {@link onErrorResumeNext}
94 * @see {@link repeat}
95 * @see {@link repeatWhen}
96 * @see {@link retry }
97 * @see {@link retryWhen}
98 *
99 * @param selector A function that takes as arguments `err`, which is the error, and `caught`, which
100 * is the source observable, in case you'd like to "retry" that observable by returning it again.
101 * Whatever observable is returned by the `selector` will be used to continue the observable chain.
102 * @return A function that returns an Observable that originates from either
103 * the source or the Observable returned by the `selector` function.
104 */
105 export function catchError<T, O extends ObservableInput<any>>(
106 selector: (err: any, caught: Observable<T>) => O
107 ): OperatorFunction<T, T | ObservedValueOf<O>> {
108 return operate((source, subscriber) => {
109 let innerSub: Subscription | null = null;
110 let syncUnsub = false;
111 let handledResult: Observable<ObservedValueOf<O>>;
112
113 innerSub = source.subscribe(
114 createOperatorSubscriber(subscriber, undefined, undefined, (err) => {
115 handledResult = innerFrom(selector(err, catchError(selector)(source)));
116 if (innerSub) {
117 innerSub.unsubscribe();
118 innerSub = null;
119 handledResult.subscribe(subscriber);
120 } else {
121 // We don't have an innerSub yet, that means the error was synchronous
122 // because the subscribe call hasn't returned yet.
123 syncUnsub = true;
124 }
125 })
126 );
127
128 if (syncUnsub) {
129 // We have a synchronous error, we need to make sure to
130 // finalize right away. This ensures that callbacks in the `finalize` operator are called
131 // at the right time, and that finalization occurs at the expected
132 // time between the source error and the subscription to the
133 // next observable.
134 innerSub.unsubscribe();
135 innerSub = null;
136 handledResult!.subscribe(subscriber);
137 }
138 });
139 }