Skip to content

Commit

Permalink
Fix the error thrown from generator is swallowed (#923)
Browse files Browse the repository at this point in the history
  • Loading branch information
adoyle-h authored and SBoudrias committed Apr 19, 2016
1 parent edfa8a0 commit 634a9c7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ gulp.task('test', ['pre-test'], function (cb) {

gulp.src('test/*.js')
.pipe(plumber())
.pipe(mocha({reporter: 'spec'}))
.pipe(mocha({reporter: 'spec', timeout: 3000}))
.on('error', function (err) {
mochaErr = err;
})
Expand Down
6 changes: 5 additions & 1 deletion lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,14 @@ Base.prototype.run = function run(cb) {

return method.apply(self, self.args);
},
// runAsync return a promise which wraps first function.
// So ensure the callback function never throw an error.
function (err) {
if (err) {
debug('An error occured while running ' + methodName, err);
self.emit('error', err);
if (self.listeners('error').length > 0) {
self.emit('error', err);
}
cb(err);
return;
}
Expand Down
16 changes: 16 additions & 0 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,22 @@ describe('generators.Base', function () {
});
});

it('stop queue processing once an error is thrown and "error" event has no listeners', function (done) {
var error = new Error();
var spy = sinon.spy();

this.TestGenerator.prototype.throwing = function () {
throw error;
};
this.TestGenerator.prototype.afterError = spy;

this.testGen.run(function (err) {
assert.equal(err, error);
sinon.assert.notCalled(spy);
done();
});
});

it('handle function returning promises as asynchronous', function (done) {
var spy1 = sinon.spy();
var spy2 = sinon.spy();
Expand Down

0 comments on commit 634a9c7

Please sign in to comment.