Loading lib/rp.js +3 −1 Original line number Original line Diff line number Diff line Loading @@ -6,7 +6,8 @@ var Bluebird = require('./bluebird-fresh.js'), Bluebird.onPossiblyUnhandledRejection(function (reason, promise) { 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); printRejectionReason(reason); } } // else: The user did not call .then(...) // else: The user did not call .then(...) Loading Loading @@ -103,6 +104,7 @@ request.Request.prototype.init = function (options) { self._rp_resolve = resolve; self._rp_resolve = resolve; self._rp_reject = reject; self._rp_reject = reject; }); }); self._rp_promise._rp_then_invoked = false; self._rp_callbackOrig = self.callback; self._rp_callbackOrig = self.callback; self.callback = ownCallback; self.callback = ownCallback; Loading test/spec/request-test.js +86 −0 Original line number Original line Diff line number Diff line Loading @@ -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 () { describe("should not alter Request's original behavior", function () { it('for emitting errors with no listener', function () { it('for emitting errors with no listener', function () { Loading Loading
lib/rp.js +3 −1 Original line number Original line Diff line number Diff line Loading @@ -6,7 +6,8 @@ var Bluebird = require('./bluebird-fresh.js'), Bluebird.onPossiblyUnhandledRejection(function (reason, promise) { 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); printRejectionReason(reason); } } // else: The user did not call .then(...) // else: The user did not call .then(...) Loading Loading @@ -103,6 +104,7 @@ request.Request.prototype.init = function (options) { self._rp_resolve = resolve; self._rp_resolve = resolve; self._rp_reject = reject; self._rp_reject = reject; }); }); self._rp_promise._rp_then_invoked = false; self._rp_callbackOrig = self.callback; self._rp_callbackOrig = self.callback; self.callback = ownCallback; self.callback = ownCallback; Loading
test/spec/request-test.js +86 −0 Original line number Original line Diff line number Diff line Loading @@ -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 () { describe("should not alter Request's original behavior", function () { it('for emitting errors with no listener', function () { it('for emitting errors with no listener', function () { Loading