1 | import { Observable } from '../Observable';
|
---|
2 | import { SchedulerLike } from '../types';
|
---|
3 | import { Subscriber } from '../Subscriber';
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * Creates an Observable that emits no items to the Observer and immediately
|
---|
7 | * emits an error notification.
|
---|
8 | *
|
---|
9 | * <span class="informal">Just emits 'error', and nothing else.
|
---|
10 | * </span>
|
---|
11 | *
|
---|
12 | * ![](throw.png)
|
---|
13 | *
|
---|
14 | * This static operator is useful for creating a simple Observable that only
|
---|
15 | * emits the error notification. It can be used for composing with other
|
---|
16 | * Observables, such as in a {@link mergeMap}.
|
---|
17 | *
|
---|
18 | * ## Examples
|
---|
19 | * ### Emit the number 7, then emit an error
|
---|
20 | * ```ts
|
---|
21 | * import { throwError, concat, of } from 'rxjs';
|
---|
22 | *
|
---|
23 | * const result = concat(of(7), throwError(new Error('oops!')));
|
---|
24 | * result.subscribe(x => console.log(x), e => console.error(e));
|
---|
25 | *
|
---|
26 | * // Logs:
|
---|
27 | * // 7
|
---|
28 | * // Error: oops!
|
---|
29 | * ```
|
---|
30 | *
|
---|
31 | * ---
|
---|
32 | *
|
---|
33 | * ### Map and flatten numbers to the sequence 'a', 'b', 'c', but throw an error for 2
|
---|
34 | * ```ts
|
---|
35 | * import { throwError, interval, of } from 'rxjs';
|
---|
36 | * import { mergeMap } from 'rxjs/operators';
|
---|
37 | *
|
---|
38 | * interval(1000).pipe(
|
---|
39 | * mergeMap(x => x === 2
|
---|
40 | * ? throwError('Twos are bad')
|
---|
41 | * : of('a', 'b', 'c')
|
---|
42 | * ),
|
---|
43 | * ).subscribe(x => console.log(x), e => console.error(e));
|
---|
44 | *
|
---|
45 | * // Logs:
|
---|
46 | * // a
|
---|
47 | * // b
|
---|
48 | * // c
|
---|
49 | * // a
|
---|
50 | * // b
|
---|
51 | * // c
|
---|
52 | * // Twos are bad
|
---|
53 | * ```
|
---|
54 | *
|
---|
55 | * @see {@link Observable}
|
---|
56 | * @see {@link empty}
|
---|
57 | * @see {@link never}
|
---|
58 | * @see {@link of}
|
---|
59 | *
|
---|
60 | * @param {any} error The particular Error to pass to the error notification.
|
---|
61 | * @param {SchedulerLike} [scheduler] A {@link SchedulerLike} to use for scheduling
|
---|
62 | * the emission of the error notification.
|
---|
63 | * @return {Observable} An error Observable: emits only the error notification
|
---|
64 | * using the given error argument.
|
---|
65 | * @static true
|
---|
66 | * @name throwError
|
---|
67 | * @owner Observable
|
---|
68 | */
|
---|
69 | export function throwError(error: any, scheduler?: SchedulerLike): Observable<never> {
|
---|
70 | if (!scheduler) {
|
---|
71 | return new Observable(subscriber => subscriber.error(error));
|
---|
72 | } else {
|
---|
73 | return new Observable(subscriber => scheduler.schedule(dispatch, 0, { error, subscriber }));
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | interface DispatchArg {
|
---|
78 | error: any;
|
---|
79 | subscriber: Subscriber<any>;
|
---|
80 | }
|
---|
81 |
|
---|
82 | function dispatch({ error, subscriber }: DispatchArg) {
|
---|
83 | subscriber.error(error);
|
---|
84 | }
|
---|