首页 手册 参考 源码 测试 关于中文版Repository
暗色主题亮色主题

es6/operator/catch.js

  1. import { OuterSubscriber } from '../OuterSubscriber';
  2. import { subscribeToResult } from '../util/subscribeToResult';
  3. /**
  4. * 捕获 observable 中的错误,可以通过返回一个新的 observable 或者抛出错误对象来处理。
  5. *
  6. * <img src="./img/catch.png" width="100%">
  7. *
  8. * @example <caption>当发生错误的时候通过返回一个新的 Observable 继续运行</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>当发生错误的时候重试源 Observable, 和retry()操作符类似</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>当源 Observable 发生错误的时候,抛出一个新的错误</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 该函数接受 `err` 参数,即错误对象,还接受 `caught` 参数,即源 Observable,
  54. * 当你想“重试”的时候返回它即可。 无论 `selector` 函数返回的 observable 是什么,都会被用来继续执行 observable 链。
  55. * @return {Observable} 该 Observable 源自源 Observable 或 selector 函数返回的 Observable。
  56. * @method catch
  57. * @name catch
  58. * @owner Observable
  59. */
  60. export function _catch(selector) {
  61. const operator = new CatchOperator(selector);
  62. const caught = this.lift(operator);
  63. return (operator.caught = caught);
  64. }
  65. class CatchOperator {
  66. constructor(selector) {
  67. this.selector = selector;
  68. }
  69. call(subscriber, source) {
  70. return source.subscribe(new CatchSubscriber(subscriber, this.selector, this.caught));
  71. }
  72. }
  73. /**
  74. * We need this JSDoc comment for affecting ESDoc.
  75. * @ignore
  76. * @extends {Ignored}
  77. */
  78. class CatchSubscriber extends OuterSubscriber {
  79. constructor(destination, selector, caught) {
  80. super(destination);
  81. this.selector = selector;
  82. this.caught = caught;
  83. }
  84. // NOTE: overriding `error` instead of `_error` because we don't want
  85. // to have this flag this subscriber as `isStopped`. We can mimic the
  86. // behavior of the RetrySubscriber (from the `retry` operator), where
  87. // we unsubscribe from our source chain, reset our Subscriber flags,
  88. // then subscribe to the selector result.
  89. error(err) {
  90. if (!this.isStopped) {
  91. let result;
  92. try {
  93. result = this.selector(err, this.caught);
  94. }
  95. catch (err2) {
  96. super.error(err2);
  97. return;
  98. }
  99. this._unsubscribeAndRecycle();
  100. this.add(subscribeToResult(this, result));
  101. }
  102. }
  103. }
  104. //# sourceMappingURL=catch.js.map