UNPKG

4.13 kBTypeScriptView Raw
1import { Observable } from '../Observable';
2import { SchedulerLike } from '../types';
3/**
4 * Creates an observable that will create an error instance and push it to the consumer as an error
5 * immediately upon subscription.
6 *
7 * <span class="informal">Just errors and does nothing else</span>
8 *
9 * ![](throw.png)
10 *
11 * This creation function is useful for creating an observable that will create an error and error every
12 * time it is subscribed to. Generally, inside of most operators when you might want to return an errored
13 * observable, this is unnecessary. In most cases, such as in the inner return of {@link concatMap},
14 * {@link mergeMap}, {@link defer}, and many others, you can simply throw the error, and RxJS will pick
15 * that up and notify the consumer of the error.
16 *
17 * ## Example
18 *
19 * Create a simple observable that will create a new error with a timestamp and log it
20 * and the message every time you subscribe to it.
21 *
22 * ```ts
23 * import { throwError } from 'rxjs';
24 *
25 * let errorCount = 0;
26 *
27 * const errorWithTimestamp$ = throwError(() => {
28 * const error: any = new Error(`This is error number ${++errorCount}`);
29 * error.timestamp = Date.now();
30 * return error;
31 * });
32 *
33 * errorWithTimestamp$.subscribe({
34 * error: err => console.log(err.timestamp, err.message)
35 * });
36 *
37 * errorWithTimestamp$.subscribe({
38 * error: err => console.log(err.timestamp, err.message)
39 * });
40 *
41 * // Logs the timestamp and a new error message each subscription;
42 * ```
43 *
44 * ## Unnecessary usage
45 *
46 * Using `throwError` inside of an operator or creation function
47 * with a callback, is usually not necessary:
48 *
49 * ```ts
50 * import { throwError, timer, of } from 'rxjs';
51 * import { concatMap } from 'rxjs/operators';
52 *
53 * const delays$ = of(1000, 2000, Infinity, 3000);
54 *
55 * delays$.pipe(
56 * concatMap(ms => {
57 * if (ms < 10000) {
58 * return timer(ms);
59 * } else {
60 * // This is probably overkill.
61 * return throwError(() => new Error(`Invalid time ${ms}`));
62 * }
63 * })
64 * )
65 * .subscribe({
66 * next: console.log,
67 * error: console.error
68 * });
69 * ```
70 *
71 * You can just throw the error instead:
72 *
73 * ```ts
74 * import { throwError, timer, of } from 'rxjs';
75 * import { concatMap } from 'rxjs/operators';
76 *
77 * const delays$ = of(1000, 2000, Infinity, 3000);
78 *
79 * delays$.pipe(
80 * concatMap(ms => {
81 * if (ms < 10000) {
82 * return timer(ms);
83 * } else {
84 * // Cleaner and easier to read for most folks.
85 * throw new Error(`Invalid time ${ms}`);
86 * }
87 * })
88 * )
89 * .subscribe({
90 * next: console.log,
91 * error: console.error
92 * });
93 * ```
94 *
95 * @param errorFactory A factory function that will create the error instance that is pushed.
96 */
97export declare function throwError(errorFactory: () => any): Observable<never>;
98/**
99 * Returns an observable that will error with the specified error immediately upon subscription.
100 *
101 * @param error The error instance to emit
102 * @deprecated Support for passing an error value will be removed in v8. Instead, pass a factory function to `throwError(() => new Error('test'))`. This is
103 * because it will create the error at the moment it should be created and capture a more appropriate stack trace. If
104 * for some reason you need to create the error ahead of time, you can still do that: `const err = new Error('test'); throwError(() => err);`.
105 */
106export declare function throwError(error: any): Observable<never>;
107/**
108 * Notifies the consumer of an error using a given scheduler by scheduling it at delay `0` upon subscription.
109 *
110 * @param errorOrErrorFactory An error instance or error factory
111 * @param scheduler A scheduler to use to schedule the error notification
112 * @deprecated The `scheduler` parameter will be removed in v8.
113 * Use `throwError` in combination with {@link observeOn}: `throwError(() => new Error('test')).pipe(observeOn(scheduler));`.
114 * Details: https://rxjs.dev/deprecations/scheduler-argument
115 */
116export declare function throwError(errorOrErrorFactory: any, scheduler: SchedulerLike): Observable<never>;
117//# sourceMappingURL=throwError.d.ts.map
\No newline at end of file