Skip to content

Commit

Permalink
Improve exception handling in parseYargs
Browse files Browse the repository at this point in the history
The previous function did not handle exceptions thrown by callback (or
after callback is invoked) correctly and caused callback to be called a
second time.  Fix this.

Signed-off-by: Kevin Locke <kevin@kevinlocke.name>
  • Loading branch information
kevinoid committed Feb 10, 2017
1 parent e085b56 commit 68ae670
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,22 @@ var modulename = require('..');
* @private
*/
function parseYargs(yargs, args, callback) {
// Since yargs doesn't nextTick its callback, this function must be careful
// that exceptions thrown from callback (which propagate through yargs.parse)
// are not caught and passed to a second invocation of callback.
var called = false;
try {
yargs.parse(args, callback);
yargs.parse(args, function() {
called = true;
return callback.apply(this, arguments);
});
} catch (err) {
// Since yargs doesn't nextTick its callback, don't here either
callback(err);
if (called) {
// err was thrown after or by callback. Let it propagate.
throw err;
} else {
callback(err);
}
}
}

Expand Down

0 comments on commit 68ae670

Please sign in to comment.