Skip to content

Commit

Permalink
Use nodejs domain error handler instead of uncaughtError handle
Browse files Browse the repository at this point in the history
See angular/protractor#876 (specifically [my
comment](angular/protractor#876 (comment))). 
Cucumber uses the top level uncaught error handler to catch exceptions
caught by tests.  Refer commit
cucumber@9091129
and issue cucumber#51.  A good approach would be to wrap the test code
executions in try/catch blocks and use that exception instead of the
`uncaughtException` handler.  Another approach is to use the error
handler of a nodejs domain instead of the top level global
uncaughtException handler.  In this commit, I'm using the domain level
error handler approach.
  • Loading branch information
chirayuk committed Oct 4, 2014
1 parent b58fdbe commit 54c2590
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions lib/cucumber/cli.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
var cucumberDomain = require('domain').create();

var Cli = function(argv) {
var Cucumber = require('../cucumber');

var self = {
run: function run(callback) {
run: cucumberDomain.run(function run(callback) {
var configuration = Cli.Configuration(argv);
if (configuration.isHelpRequested())
self.displayHelp(callback);
else if (configuration.isVersionRequested())
self.displayVersion(callback);
else
self.runSuiteWithConfiguration(configuration, callback);
},
}),

runSuiteWithConfiguration: function runSuiteWithConfiguration(configuration, callback) {
var runtime = Cucumber.Runtime(configuration);
Expand Down
8 changes: 6 additions & 2 deletions lib/cucumber/util/exception.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
var Exception = {
registerUncaughtExceptionHandler: function registerUncaughtExceptionHandler(exceptionHandler) {
if (process.on)
if (process.domain)
process.domain.on('error', exceptionHandler);
else if (process.on)
process.on('uncaughtException', exceptionHandler);
else if (typeof(window) != 'undefined')
window.onerror = exceptionHandler;
},

unregisterUncaughtExceptionHandler: function unregisterUncaughtExceptionHandler(exceptionHandler) {
if (process.removeListener)
if (process.domain)
process.domain.removeListener('error', exceptionHandler);
else if (process.removeListener)
process.removeListener('uncaughtException', exceptionHandler);
else if (typeof(window) != 'undefined')
window.onerror = void(0);
Expand Down

0 comments on commit 54c2590

Please sign in to comment.