UNPKG

4.03 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 for 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 { of, concatMap, timer, throwError } from 'rxjs';
51 *
52 * const delays$ = of(1000, 2000, Infinity, 3000);
53 *
54 * delays$.pipe(
55 * concatMap(ms => {
56 * if (ms < 10000) {
57 * return timer(ms);
58 * } else {
59 * // This is probably overkill.
60 * return throwError(() => new Error(`Invalid time ${ ms }`));
61 * }
62 * })
63 * )
64 * .subscribe({
65 * next: console.log,
66 * error: console.error
67 * });
68 * ```
69 *
70 * You can just throw the error instead
71 *
72 * ```ts
73 * import { of, concatMap, timer } from 'rxjs';
74 *
75 * const delays$ = of(1000, 2000, Infinity, 3000);
76 *
77 * delays$.pipe(
78 * concatMap(ms => {
79 * if (ms < 10000) {
80 * return timer(ms);
81 * } else {
82 * // Cleaner and easier to read for most folks.
83 * throw new Error(`Invalid time ${ ms }`);
84 * }
85 * })
86 * )
87 * .subscribe({
88 * next: console.log,
89 * error: console.error
90 * });
91 * ```
92 *
93 * @param errorFactory A factory function that will create the error instance that is pushed.
94 */
95export declare function throwError(errorFactory: () => any): Observable<never>;
96/**
97 * Returns an observable that will error with the specified error immediately upon subscription.
98 *
99 * @param error The error instance to emit
100 * @deprecated Support for passing an error value will be removed in v8. Instead, pass a factory function to `throwError(() => new Error('test'))`. This is
101 * because it will create the error at the moment it should be created and capture a more appropriate stack trace. If
102 * for some reason you need to create the error ahead of time, you can still do that: `const err = new Error('test'); throwError(() => err);`.
103 */
104export declare function throwError(error: any): Observable<never>;
105/**
106 * Notifies the consumer of an error using a given scheduler by scheduling it at delay `0` upon subscription.
107 *
108 * @param errorOrErrorFactory An error instance or error factory
109 * @param scheduler A scheduler to use to schedule the error notification
110 * @deprecated The `scheduler` parameter will be removed in v8.
111 * Use `throwError` in combination with {@link observeOn}: `throwError(() => new Error('test')).pipe(observeOn(scheduler));`.
112 * Details: https://rxjs.dev/deprecations/scheduler-argument
113 */
114export declare function throwError(errorOrErrorFactory: any, scheduler: SchedulerLike): Observable<never>;
115//# sourceMappingURL=throwError.d.ts.map
\No newline at end of file