catchError.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /** PURE_IMPORTS_START .._OuterSubscriber,.._util_subscribeToResult PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { OuterSubscriber } from '../OuterSubscriber';
  10. import { subscribeToResult } from '../util/subscribeToResult';
  11. /**
  12. * Catches errors on the observable to be handled by returning a new observable or throwing an error.
  13. *
  14. * <img src="./img/catch.png" width="100%">
  15. *
  16. * @example <caption>Continues with a different Observable when there's an error</caption>
  17. *
  18. * Observable.of(1, 2, 3, 4, 5)
  19. * .map(n => {
  20. * if (n == 4) {
  21. * throw 'four!';
  22. * }
  23. * return n;
  24. * })
  25. * .catch(err => Observable.of('I', 'II', 'III', 'IV', 'V'))
  26. * .subscribe(x => console.log(x));
  27. * // 1, 2, 3, I, II, III, IV, V
  28. *
  29. * @example <caption>Retries the caught source Observable again in case of error, similar to retry() operator</caption>
  30. *
  31. * Observable.of(1, 2, 3, 4, 5)
  32. * .map(n => {
  33. * if (n === 4) {
  34. * throw 'four!';
  35. * }
  36. * return n;
  37. * })
  38. * .catch((err, caught) => caught)
  39. * .take(30)
  40. * .subscribe(x => console.log(x));
  41. * // 1, 2, 3, 1, 2, 3, ...
  42. *
  43. * @example <caption>Throws a new error when the source Observable throws an error</caption>
  44. *
  45. * Observable.of(1, 2, 3, 4, 5)
  46. * .map(n => {
  47. * if (n == 4) {
  48. * throw 'four!';
  49. * }
  50. * return n;
  51. * })
  52. * .catch(err => {
  53. * throw 'error in source. Details: ' + err;
  54. * })
  55. * .subscribe(
  56. * x => console.log(x),
  57. * err => console.log(err)
  58. * );
  59. * // 1, 2, 3, error in source. Details: four!
  60. *
  61. * @param {function} selector a function that takes as arguments `err`, which is the error, and `caught`, which
  62. * is the source observable, in case you'd like to "retry" that observable by returning it again. Whatever observable
  63. * is returned by the `selector` will be used to continue the observable chain.
  64. * @return {Observable} An observable that originates from either the source or the observable returned by the
  65. * catch `selector` function.
  66. * @name catchError
  67. */
  68. export function catchError(selector) {
  69. return function catchErrorOperatorFunction(source) {
  70. var operator = new CatchOperator(selector);
  71. var caught = source.lift(operator);
  72. return (operator.caught = caught);
  73. };
  74. }
  75. var CatchOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  76. function CatchOperator(selector) {
  77. this.selector = selector;
  78. }
  79. CatchOperator.prototype.call = function (subscriber, source) {
  80. return source.subscribe(new CatchSubscriber(subscriber, this.selector, this.caught));
  81. };
  82. return CatchOperator;
  83. }());
  84. /**
  85. * We need this JSDoc comment for affecting ESDoc.
  86. * @ignore
  87. * @extends {Ignored}
  88. */
  89. var CatchSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  90. __extends(CatchSubscriber, _super);
  91. function CatchSubscriber(destination, selector, caught) {
  92. _super.call(this, destination);
  93. this.selector = selector;
  94. this.caught = caught;
  95. }
  96. // NOTE: overriding `error` instead of `_error` because we don't want
  97. // to have this flag this subscriber as `isStopped`. We can mimic the
  98. // behavior of the RetrySubscriber (from the `retry` operator), where
  99. // we unsubscribe from our source chain, reset our Subscriber flags,
  100. // then subscribe to the selector result.
  101. CatchSubscriber.prototype.error = function (err) {
  102. if (!this.isStopped) {
  103. var result = void 0;
  104. try {
  105. result = this.selector(err, this.caught);
  106. }
  107. catch (err2) {
  108. _super.prototype.error.call(this, err2);
  109. return;
  110. }
  111. this._unsubscribeAndRecycle();
  112. this.add(subscribeToResult(this, result));
  113. }
  114. };
  115. return CatchSubscriber;
  116. }(OuterSubscriber));
  117. //# sourceMappingURL=catchError.js.map