Commit d81e1415 authored by analog-nico's avatar analog-nico
Browse files

Added .catch() support

parent c164258f
Loading
Loading
Loading
Loading
+25 −15
Original line number Diff line number Diff line
@@ -5,16 +5,6 @@ var Bluebird = require('./bluebird-fresh.js'),
    _ = require('lodash');


Bluebird.onPossiblyUnhandledRejection(function (reason, promise) {
    // For whatever reason we don't see _rp_then_invoked here at all after then is called. --> We compare to false instead of true.
    if (promise._rp_then_invoked !== false) {
        printRejectionReason(reason);
    }
    // else: The user did not call .then(...)
    // --> We need to assume that this request is processed with a callback or a pipe etc.
});


// Load Request freshly - so that users can require an unaltered request instance!
var request = (function () {

@@ -85,7 +75,7 @@ function ownCallback(err, httpResponse, body) {
    }

    // Mimic original behavior of errors emitted by request with no error listener registered
    if (err && _.isFunction(self._rp_callbackOrig) === false && self._rp_then_invoked !== true && self.listeners('error').length === 1) {
    if (err && _.isFunction(self._rp_callbackOrig) === false && self._rp_promise_in_use !== true && self.listeners('error').length === 1) {
        throw err;
    }

@@ -104,7 +94,7 @@ request.Request.prototype.init = function (options) {
            self._rp_resolve = resolve;
            self._rp_reject = reject;
        });
        self._rp_promise._rp_then_invoked = false;
        self._rp_promise._rp_in_use = false;

        self._rp_callbackOrig = self.callback;
        self.callback = ownCallback;
@@ -123,10 +113,30 @@ request.Request.prototype.init = function (options) {

};

function markPromiseInUse(requestInstance) {
    requestInstance._rp_promise_in_use = true;
    requestInstance._rp_promise._rp_in_use = true;
}

request.Request.prototype.then = function (onFulfilled, onRejected) {
    this._rp_then_invoked = true;
    this._rp_promise._rp_then_invoked = true;
    return this._rp_promise.then(onFulfilled, onRejected);
    markPromiseInUse(this);
    return this._rp_promise.then.apply(this._rp_promise, arguments);
};

request.Request.prototype.catch = function (onRejected) {
    markPromiseInUse(this);
    return this._rp_promise.catch.apply(this._rp_promise, arguments);
};


Bluebird.onPossiblyUnhandledRejection(function (reason, promise) {
    // For whatever reason we don't see _rp_in_use here at all after then is called. --> We compare to false instead of true.
    if (promise._rp_in_use !== false) {
        printRejectionReason(reason);
    }
    // else: The user did not call .then(...)
    // --> We need to assume that this request is processed with a callback or a pipe etc.
});


module.exports = request;
+25 −0
Original line number Diff line number Diff line
@@ -634,6 +634,31 @@ describe('Request-Promise', function () {

    });

    describe('should expose additional Bluebird methods', function () {

        it('.catch(Function handler)', function (done) {

            rp('http://localhost:4000/404')
                .catch(function (reason) {
                    done();
                });

        });

        it('.catch([Function ErrorClass|Function predicate...], Function handler)', function (done) {

            rp({ uri: 'http://localhost:4000/200', transform: function () { throw new Error('Transform failed.'); } })
                .catch(Error, function (err) {
                    done();
                })
                .catch(function (reason) {
                    done(new Error('Expected rejection reason to be an Error object.'));
                });

        });

    });

    describe('should handle possibly unhandled rejections', function () {

        var origStderrWrite, stderr;