work.suroh.tk/node_modules/rxjs/operators/catchError.js

116 lines
3.9 KiB
JavaScript

"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var OuterSubscriber_1 = require('../OuterSubscriber');
var subscribeToResult_1 = require('../util/subscribeToResult');
/**
* Catches errors on the observable to be handled by returning a new observable or throwing an error.
*
* <img src="./img/catch.png" width="100%">
*
* @example <caption>Continues with a different Observable when there's an error</caption>
*
* Observable.of(1, 2, 3, 4, 5)
* .map(n => {
* if (n == 4) {
* throw 'four!';
* }
* return n;
* })
* .catch(err => Observable.of('I', 'II', 'III', 'IV', 'V'))
* .subscribe(x => console.log(x));
* // 1, 2, 3, I, II, III, IV, V
*
* @example <caption>Retries the caught source Observable again in case of error, similar to retry() operator</caption>
*
* Observable.of(1, 2, 3, 4, 5)
* .map(n => {
* if (n === 4) {
* throw 'four!';
* }
* return n;
* })
* .catch((err, caught) => caught)
* .take(30)
* .subscribe(x => console.log(x));
* // 1, 2, 3, 1, 2, 3, ...
*
* @example <caption>Throws a new error when the source Observable throws an error</caption>
*
* Observable.of(1, 2, 3, 4, 5)
* .map(n => {
* if (n == 4) {
* throw 'four!';
* }
* return n;
* })
* .catch(err => {
* throw 'error in source. Details: ' + err;
* })
* .subscribe(
* x => console.log(x),
* err => console.log(err)
* );
* // 1, 2, 3, error in source. Details: four!
*
* @param {function} selector a function that takes as arguments `err`, which is the error, and `caught`, which
* is the source observable, in case you'd like to "retry" that observable by returning it again. Whatever observable
* is returned by the `selector` will be used to continue the observable chain.
* @return {Observable} An observable that originates from either the source or the observable returned by the
* catch `selector` function.
* @name catchError
*/
function catchError(selector) {
return function catchErrorOperatorFunction(source) {
var operator = new CatchOperator(selector);
var caught = source.lift(operator);
return (operator.caught = caught);
};
}
exports.catchError = catchError;
var CatchOperator = (function () {
function CatchOperator(selector) {
this.selector = selector;
}
CatchOperator.prototype.call = function (subscriber, source) {
return source.subscribe(new CatchSubscriber(subscriber, this.selector, this.caught));
};
return CatchOperator;
}());
/**
* We need this JSDoc comment for affecting ESDoc.
* @ignore
* @extends {Ignored}
*/
var CatchSubscriber = (function (_super) {
__extends(CatchSubscriber, _super);
function CatchSubscriber(destination, selector, caught) {
_super.call(this, destination);
this.selector = selector;
this.caught = caught;
}
// NOTE: overriding `error` instead of `_error` because we don't want
// to have this flag this subscriber as `isStopped`. We can mimic the
// behavior of the RetrySubscriber (from the `retry` operator), where
// we unsubscribe from our source chain, reset our Subscriber flags,
// then subscribe to the selector result.
CatchSubscriber.prototype.error = function (err) {
if (!this.isStopped) {
var result = void 0;
try {
result = this.selector(err, this.caught);
}
catch (err2) {
_super.prototype.error.call(this, err2);
return;
}
this._unsubscribeAndRecycle();
this.add(subscribeToResult_1.subscribeToResult(this, result));
}
};
return CatchSubscriber;
}(OuterSubscriber_1.OuterSubscriber));
//# sourceMappingURL=catchError.js.map