Skip to content

Commit

Permalink
Fix yeoman-generator ignoring errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SBoudrias committed May 3, 2016
1 parent 7d0aa14 commit f733835
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 40 deletions.
39 changes: 16 additions & 23 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,29 +423,22 @@ Base.prototype.run = function run(cb) {
debug('Running ' + methodName);
self.emit('method:' + methodName);

runAsync(
function () {
self.async = function () {
return this.async();
}.bind(this);

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);
if (self.listeners('error').length > 0) {
self.emit('error', err);
}
cb(err);
return;
}

completed();
}
)();
runAsync(function () {
self.async = function () {
return this.async();
}.bind(this);

return method.apply(self, self.args);
})().then(completed).catch(function (err) {
debug('An error occured while running ' + methodName, err);

// Ensure we emit the error event outside the promise context so it won't be
// swallowed when there's no listeners.
setImmediate(function () {
self.emit('error', err);
cb(err);
});
});
});
}

Expand Down
18 changes: 1 addition & 17 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,23 +319,7 @@ describe('generators.Base', function () {
this.testGen.on('error', sinon.spy());
this.testGen.run(function (err) {
assert.equal(err, error);
sinon.assert.notCalled(spy);
done();
});
});

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);
sinon.assert.called(spy);
done();
});
});
Expand Down

0 comments on commit f733835

Please sign in to comment.