catchError.js 3.4 KB

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