From f73383521e67b5859b8a9fc2ed2fea100359029e Mon Sep 17 00:00:00 2001 From: Simon Boudrias Date: Tue, 3 May 2016 00:29:42 -0700 Subject: [PATCH] Fix yeoman-generator ignoring errors --- lib/base.js | 39 ++++++++++++++++----------------------- test/base.js | 18 +----------------- 2 files changed, 17 insertions(+), 40 deletions(-) diff --git a/lib/base.js b/lib/base.js index e1c2bff0..4807e9de 100644 --- a/lib/base.js +++ b/lib/base.js @@ -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); + }); + }); }); } diff --git a/test/base.js b/test/base.js index db771ce8..cd058d3c 100644 --- a/test/base.js +++ b/test/base.js @@ -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(); }); });