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

Removed special handling of unhandled rejection - solves issue #43

parent 485865d1
Loading
Loading
Loading
Loading
+0 −1
Original line number Original line Diff line number Diff line
module.exports = require("bluebird/js/main/captured_trace")();

lib/bluebird-fresh.js

deleted100644 → 0
+0 −2
Original line number Original line Diff line number Diff line
// See: https://github.com/petkaantonov/bluebird#for-library-authors
module.exports = require("bluebird/js/main/promise")();
+1 −27
Original line number Original line Diff line number Diff line
'use strict';
'use strict';


var Bluebird = require('./bluebird-fresh.js'),
var Bluebird = require('bluebird'),
    CapturedTrace = require('./bluebird-captured-trace-fresh.js'),
    assign = require('lodash/object/assign'),
    assign = require('lodash/object/assign'),
    forEach = require('lodash/collection/forEach'),
    forEach = require('lodash/collection/forEach'),
    isFunction = require('lodash/lang/isFunction'),
    isFunction = require('lodash/lang/isFunction'),
@@ -85,11 +84,6 @@ function RP$callback(err, response, body) {
        throw thrownException;
        throw thrownException;
    }
    }


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

}
}


var originalInit = request.Request.prototype.init;
var originalInit = request.Request.prototype.init;
@@ -105,7 +99,6 @@ request.Request.prototype.init = function RP$initInterceptor(options) {
            self._rp_resolve = resolve;
            self._rp_resolve = resolve;
            self._rp_reject = reject;
            self._rp_reject = reject;
        });
        });
        self._rp_promise._rp_in_use = false;


        self._rp_callbackOrig = self.callback;
        self._rp_callbackOrig = self.callback;
        self.callback = RP$callback;
        self.callback = RP$callback;
@@ -124,11 +117,6 @@ request.Request.prototype.init = function RP$initInterceptor(options) {


};
};


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

function expose(methodToExpose, exposeAs) {
function expose(methodToExpose, exposeAs) {


    exposeAs = exposeAs || methodToExpose;
    exposeAs = exposeAs || methodToExpose;
@@ -140,7 +128,6 @@ function expose(methodToExpose, exposeAs) {
    }
    }


    request.Request.prototype[exposeAs] = function RP$exposed() {
    request.Request.prototype[exposeAs] = function RP$exposed() {
        markPromiseInUse(this);
        return this._rp_promise[methodToExpose].apply(this._rp_promise, arguments);
        return this._rp_promise[methodToExpose].apply(this._rp_promise, arguments);
    };
    };


@@ -151,21 +138,8 @@ expose('catch');
expose('finally');
expose('finally');


request.Request.prototype.promise = function RP$promise() {
request.Request.prototype.promise = function RP$promise() {
    markPromiseInUse(this);
    return this._rp_promise;
    return this._rp_promise;
};
};




var printRejectionReason = CapturedTrace.formatAndLogError || CapturedTrace.possiblyUnhandledRejection;

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, 'Possibly unhandled ');
    }
    // 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;
module.exports = request;
+85 −11
Original line number Original line Diff line number Diff line
@@ -726,7 +726,7 @@ describe('Request-Promise', function () {
            process.stderr.write = origStderrWrite;
            process.stderr.write = origStderrWrite;
        });
        });


        it('by muting them if .then(...) was not called yet', function (done) {
        it('by printing them if .then(...) was not called yet', function (done) {


            rp('http://localhost:1/200', function (err) {
            rp('http://localhost:1/200', function (err) {
                if (!err) {
                if (!err) {
@@ -734,8 +734,8 @@ describe('Request-Promise', function () {
                    return;
                    return;
                }
                }
                setTimeout(function () {
                setTimeout(function () {
                    if (stderr.length > 0) {
                    if (stderr.length === 0) {
                        done(new Error('Observed unexpected output to stderr.'));
                        done(new Error('Observed no output to stderr.'));
                    } else {
                    } else {
                        done();
                        done();
                    }
                    }
@@ -852,16 +852,51 @@ describe('Request-Promise', function () {


    describe("should not alter Request's original behavior", function () {
    describe("should not alter Request's original behavior", function () {


        it('for emitting errors with no listener', function () {
        describe('but also include unhandled rejections', function () {
            expect(function () {

                rp({});
            var origStderrWrite, stderr;
            }).to.throw();

            beforeEach(function () {
                origStderrWrite = process.stderr.write;
                stderr = [];
                process.stderr.write = function (string, encoding, fd) {
                    stderr.push(string);
                };
            });

            afterEach(function () {
                process.stderr.write = origStderrWrite;
            });
            });


        it('for emitting errors to the callback', function (done) {
            it('when emitting errors to the callback', function (done) {

                var counter = 2;
                function countDown() {
                    counter -= 1;
                    if (counter === 0) {
                        done();
                    }
                }

                rp({}, function (err) {
                rp({}, function (err) {
                if (err) { done(); }
                    if (err) {
                        countDown();
                    } else {
                        done(new Error('Observed no output to stderr.'));
                    }
                });
                });

                // Unhandled rejection expected
                setTimeout(function () {
                    if (stderr.length === 0) {
                        done(new Error('Observed no output to stderr.'));
                    } else {
                        countDown();
                    }
                });

            });

        });
        });


        it('for registering a handler to the emitted complete event', function (done) {
        it('for registering a handler to the emitted complete event', function (done) {
@@ -912,6 +947,45 @@ describe('Request-Promise', function () {


    });
    });


    describe("should alter Request's original behavior", function () {

        describe('by preferring unhandled rejections', function () {

            var origStderrWrite, stderr;

            beforeEach(function () {
                origStderrWrite = process.stderr.write;
                stderr = [];
                process.stderr.write = function(string, encoding, fd) {
                    stderr.push(string);
                };
            });

            afterEach(function () {
                process.stderr.write = origStderrWrite;
            });

            it('when emitting errors with no listener', function (done) {

                expect(function () {
                    rp({});
                }).to.not.throw();

                // Unhandled rejection expected
                setTimeout(function () {
                    if (stderr.length === 0) {
                        done(new Error('Observed no output to stderr.'));
                    } else {
                        done();
                    }
                });

            });

        });

    });

    describe('should still allow to require Request independently', function (done) {
    describe('should still allow to require Request independently', function (done) {


        it('by not interfering with Request required afterwards', function (done) {
        it('by not interfering with Request required afterwards', function (done) {