catchError.js 3.9KB

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