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

Finalized unhandled rejection handling

parent 0abcee84
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -6,7 +6,8 @@ var Bluebird = require('./bluebird-fresh.js'),


Bluebird.onPossiblyUnhandledRejection(function (reason, promise) {
    if (promise._rp_then_invoked === true) {
    // 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(...)
@@ -103,6 +104,7 @@ request.Request.prototype.init = function (options) {
            self._rp_resolve = resolve;
            self._rp_reject = reject;
        });
        self._rp_promise._rp_then_invoked = false;

        self._rp_callbackOrig = self.callback;
        self.callback = ownCallback;
+86 −0
Original line number Diff line number Diff line
@@ -634,6 +634,92 @@ describe('Request-Promise', function () {

    });

    describe('should handle possibly 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('by muting them if .then(...) was not called yet', function (done) {

            rp('http://localhost:1/200', function (err) {
                if (!err) {
                    done(new Error('The request should fail.'));
                    return;
                }
                setTimeout(function () {
                    if (stderr.length > 0) {
                        done(new Error('Observed unexpected output to stderr.'));
                    } else {
                        done();
                    }
                });
            });

        });

        it('by printing them if .then(...) was called without a rejection handler', function (done) {

            rp('http://localhost:1/200', function (err) {
                if (!err) {
                    done(new Error('The request should fail.'));
                    return;
                }
                setTimeout(function () {
                    if (stderr.length === 0) {
                        done(new Error('Observed no output to stderr.'));
                    } else {
                        done();
                    }
                });
            }).then(function () {}, null); // No rejection handler

        });

        it('but not printing them if .then(...) was called with a rejection handler', function (done) {

            rp('http://localhost:1/200', function (err) {
                if (!err) {
                    done(new Error('The request should fail.'));
                    return;
                }
                setTimeout(function () {
                    if (stderr.length > 0) {
                        done(new Error('Observed unexpected output to stderr.'));
                    } else {
                        done();
                    }
                });
            }).then(function () {}, function () {});

        });

        it('and not interfere with Bluebird required by the user', function (done) {

            Bluebird.reject(new Error());

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

        });

    });

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

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