Skip to content

Commit f733835

Browse files
committed
Fix yeoman-generator ignoring errors
1 parent 7d0aa14 commit f733835

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

lib/base.js

+16-23
Original file line numberDiff line numberDiff line change
@@ -423,29 +423,22 @@ Base.prototype.run = function run(cb) {
423423
debug('Running ' + methodName);
424424
self.emit('method:' + methodName);
425425

426-
runAsync(
427-
function () {
428-
self.async = function () {
429-
return this.async();
430-
}.bind(this);
431-
432-
return method.apply(self, self.args);
433-
},
434-
// runAsync return a promise which wraps first function.
435-
// So ensure the callback function never throw an error.
436-
function (err) {
437-
if (err) {
438-
debug('An error occured while running ' + methodName, err);
439-
if (self.listeners('error').length > 0) {
440-
self.emit('error', err);
441-
}
442-
cb(err);
443-
return;
444-
}
445-
446-
completed();
447-
}
448-
)();
426+
runAsync(function () {
427+
self.async = function () {
428+
return this.async();
429+
}.bind(this);
430+
431+
return method.apply(self, self.args);
432+
})().then(completed).catch(function (err) {
433+
debug('An error occured while running ' + methodName, err);
434+
435+
// Ensure we emit the error event outside the promise context so it won't be
436+
// swallowed when there's no listeners.
437+
setImmediate(function () {
438+
self.emit('error', err);
439+
cb(err);
440+
});
441+
});
449442
});
450443
}
451444

test/base.js

+1-17
Original file line numberDiff line numberDiff line change
@@ -319,23 +319,7 @@ describe('generators.Base', function () {
319319
this.testGen.on('error', sinon.spy());
320320
this.testGen.run(function (err) {
321321
assert.equal(err, error);
322-
sinon.assert.notCalled(spy);
323-
done();
324-
});
325-
});
326-
327-
it('stop queue processing once an error is thrown and "error" event has no listeners', function (done) {
328-
var error = new Error();
329-
var spy = sinon.spy();
330-
331-
this.TestGenerator.prototype.throwing = function () {
332-
throw error;
333-
};
334-
this.TestGenerator.prototype.afterError = spy;
335-
336-
this.testGen.run(function (err) {
337-
assert.equal(err, error);
338-
sinon.assert.notCalled(spy);
322+
sinon.assert.called(spy);
339323
done();
340324
});
341325
});

0 commit comments

Comments
 (0)