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