90e7f245f47dfc3c392eb001f36af9a051d0891c265bbf2eaf5b0ac26e47a2769d5e591c2f4e6e5077f1b59d8431f52d80ba4ad68045f08df9f3669dcebef8 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import { Observable } from '../Observable';
  2. import { Subscriber } from '../Subscriber';
  3. import { SchedulerLike } from '../types';
  4. import { isFunction } from '../util/isFunction';
  5. /**
  6. * Creates an observable that will create an error instance and push it to the consumer as an error
  7. * immediately upon subscription.
  8. *
  9. * <span class="informal">Just errors and does nothing else</span>
  10. *
  11. * ![](throw.png)
  12. *
  13. * This creation function is useful for creating an observable that will create an error and error every
  14. * time it is subscribed to. Generally, inside of most operators when you might want to return an errored
  15. * observable, this is unnecessary. In most cases, such as in the inner return of {@link concatMap},
  16. * {@link mergeMap}, {@link defer}, and many others, you can simply throw the error, and RxJS will pick
  17. * that up and notify the consumer of the error.
  18. *
  19. * ## Example
  20. *
  21. * Create a simple observable that will create a new error with a timestamp and log it
  22. * and the message every time you subscribe to it
  23. *
  24. * ```ts
  25. * import { throwError } from 'rxjs';
  26. *
  27. * let errorCount = 0;
  28. *
  29. * const errorWithTimestamp$ = throwError(() => {
  30. * const error: any = new Error(`This is error number ${ ++errorCount }`);
  31. * error.timestamp = Date.now();
  32. * return error;
  33. * });
  34. *
  35. * errorWithTimestamp$.subscribe({
  36. * error: err => console.log(err.timestamp, err.message)
  37. * });
  38. *
  39. * errorWithTimestamp$.subscribe({
  40. * error: err => console.log(err.timestamp, err.message)
  41. * });
  42. *
  43. * // Logs the timestamp and a new error message for each subscription
  44. * ```
  45. *
  46. * ### Unnecessary usage
  47. *
  48. * Using `throwError` inside of an operator or creation function
  49. * with a callback, is usually not necessary
  50. *
  51. * ```ts
  52. * import { of, concatMap, timer, throwError } from 'rxjs';
  53. *
  54. * const delays$ = of(1000, 2000, Infinity, 3000);
  55. *
  56. * delays$.pipe(
  57. * concatMap(ms => {
  58. * if (ms < 10000) {
  59. * return timer(ms);
  60. * } else {
  61. * // This is probably overkill.
  62. * return throwError(() => new Error(`Invalid time ${ ms }`));
  63. * }
  64. * })
  65. * )
  66. * .subscribe({
  67. * next: console.log,
  68. * error: console.error
  69. * });
  70. * ```
  71. *
  72. * You can just throw the error instead
  73. *
  74. * ```ts
  75. * import { of, concatMap, timer } from 'rxjs';
  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. */
  97. export 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. */
  106. export 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. */
  116. export function throwError(errorOrErrorFactory: any, scheduler: SchedulerLike): Observable<never>;
  117. export function throwError(errorOrErrorFactory: any, scheduler?: SchedulerLike): Observable<never> {
  118. const errorFactory = isFunction(errorOrErrorFactory) ? errorOrErrorFactory : () => errorOrErrorFactory;
  119. const init = (subscriber: Subscriber<never>) => subscriber.error(errorFactory());
  120. return new Observable(scheduler ? (subscriber) => scheduler.schedule(init as any, 0, subscriber) : init);
  121. }