Commit 3a21f12e authored by analog-nico's avatar analog-nico
Browse files

Rethrow exception thrown by the original callback

parent c5efc98b
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -60,8 +60,9 @@ gulp.task('test', ['clean'], function (done) {

            gulp.src(paths.specFiles)
                .pipe(mocha())
                .on('error', function () {
                    console.log(chalk.bold.bgRed(' TESTS FAILED '));
                .on('error', function (err) {
                    console.error(String(err));
                    console.error(chalk.bold.bgRed(' TESTS FAILED '));
                    done();
                })
                .pipe(istanbul.writeReports({
+10 −1
Original line number Diff line number Diff line
@@ -10,10 +10,15 @@ function ownCallback(err, httpResponse, body) {
    /* jshint validthis:true */
    var self = this;

    var origCallbackThrewException = false, thrownException;

    if (_.isFunction(self._rp_callbackOrig)) {
        try {
            self._rp_callbackOrig.apply(self, arguments);
        } catch (e) { }
        } catch (e) {
            origCallbackThrewException = true;
            thrownException = e;
        }
    }

    if (err) {
@@ -43,6 +48,10 @@ function ownCallback(err, httpResponse, body) {
        }
    }

    if (origCallbackThrewException) {
        throw thrownException;
    }

}

var originalInit = request.Request.prototype.init;
+24 −11
Original line number Diff line number Diff line
@@ -435,21 +435,34 @@ describe('Request-Promise', function () {

        });

        it('and still work if the callback throws an exception', function () {

            return new Bluebird(function (resolve, reject) {
        it('and still work if the callback throws an exception', function (done) {

            var callback = function (err, httpResponse, body) {
                throw new Error();
            };

            var originalErrorListeners = process.domain.listeners('error');
            process.domain.removeAllListeners('error');

            var errorCount = 0;
            process.domain.on('error', function () {
                errorCount += 1;
            });

            rp('http://localhost:4000/200', callback)
                .then(function (body) {

                    process.domain.removeAllListeners('error');
                    for ( var i = 0; i < originalErrorListeners.length; i+=1 ) {
                        process.domain.on('error', originalErrorListeners[i]);
                    }

                    expect(errorCount).to.eql(1);
                    expect(body).to.eql('GET /200');
                        resolve();
                    });
                    done();

            });
                })
                .catch(done);

        });