catch.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var operators_1 = require("rxjs/operators");
  4. /**
  5. * Catches errors on the observable to be handled by returning a new observable or throwing an error.
  6. *
  7. * <img src="./img/catch.png" width="100%">
  8. *
  9. * @example <caption>Continues with a different Observable when there's an error</caption>
  10. *
  11. * Observable.of(1, 2, 3, 4, 5)
  12. * .map(n => {
  13. * if (n == 4) {
  14. * throw 'four!';
  15. * }
  16. * return n;
  17. * })
  18. * .catch(err => Observable.of('I', 'II', 'III', 'IV', 'V'))
  19. * .subscribe(x => console.log(x));
  20. * // 1, 2, 3, I, II, III, IV, V
  21. *
  22. * @example <caption>Retries the caught source Observable again in case of error, similar to retry() operator</caption>
  23. *
  24. * Observable.of(1, 2, 3, 4, 5)
  25. * .map(n => {
  26. * if (n === 4) {
  27. * throw 'four!';
  28. * }
  29. * return n;
  30. * })
  31. * .catch((err, caught) => caught)
  32. * .take(30)
  33. * .subscribe(x => console.log(x));
  34. * // 1, 2, 3, 1, 2, 3, ...
  35. *
  36. * @example <caption>Throws a new error when the source Observable throws an error</caption>
  37. *
  38. * Observable.of(1, 2, 3, 4, 5)
  39. * .map(n => {
  40. * if (n == 4) {
  41. * throw 'four!';
  42. * }
  43. * return n;
  44. * })
  45. * .catch(err => {
  46. * throw 'error in source. Details: ' + err;
  47. * })
  48. * .subscribe(
  49. * x => console.log(x),
  50. * err => console.log(err)
  51. * );
  52. * // 1, 2, 3, error in source. Details: four!
  53. *
  54. * @param {function} selector a function that takes as arguments `err`, which is the error, and `caught`, which
  55. * is the source observable, in case you'd like to "retry" that observable by returning it again. Whatever observable
  56. * is returned by the `selector` will be used to continue the observable chain.
  57. * @return {Observable} An observable that originates from either the source or the observable returned by the
  58. * catch `selector` function.
  59. * @method catch
  60. * @name catch
  61. * @owner Observable
  62. */
  63. function _catch(selector) {
  64. return operators_1.catchError(selector)(this);
  65. }
  66. exports._catch = _catch;
  67. //# sourceMappingURL=catch.js.map