Skip to content

Commit

Permalink
Fix error reporting of lessc executable
Browse files Browse the repository at this point in the history
This commit replaces the old control flow of exiting the process when an error occurred which swallowed the error in some situations (less#2881). Additionally, it also adds a listener for "unhandledRejection" to also catch errors caused by rejected promises.
  • Loading branch information
jhnns committed Apr 28, 2016
1 parent d07a9b6 commit 0bbb1c5
Showing 1 changed file with 21 additions and 26 deletions.
47 changes: 21 additions & 26 deletions bin/lessc
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ var silent = false,
plugins: plugins
};
var sourceMapOptions = {};
var continueProcessing = true,
currentErrorcode;

// calling process.exit does not flush stdout always
// so use this to set the exit code
process.on('exit', function() { process.reallyExit(currentErrorcode); });
var exitWithError = function(msg) {
console.error(msg);
process.exit(1);
};

var exitWithLog = function(msg) {
console.log(msg);
process.exit(0);
};

var checkArgFunc = function(arg, option) {
if (!option) {
console.error(arg + " option requires a parameter");
continueProcessing = false;
currentErrorcode = 1;
exitWithError(arg + " option requires a parameter");
return false;
}
return true;
Expand All @@ -60,9 +62,7 @@ var checkArgFunc = function(arg, option) {
var checkBooleanArg = function(arg) {
var onOff = /^((on|t|true|y|yes)|(off|f|false|n|no))$/i.exec(arg);
if (!onOff) {
console.error(" unable to parse " + arg + " as a boolean. use one of on/t/true/y/yes/off/f/false/n/no");
continueProcessing = false;
currentErrorcode = 1;
exitWithError(" unable to parse " + arg + " as a boolean. use one of on/t/true/y/yes/off/f/false/n/no");
return false;
}
return Boolean(onOff[2]);
Expand All @@ -78,9 +78,12 @@ var sourceMapFileInline = false;
function printUsage() {
less.lesscHelper.printUsage();
pluginLoader.printUsage(plugins);
continueProcessing = false;
}

process.on("unhandledRejection", function(reason) {
exitWithError(reason);
});

// self executing function so we can return
(function() {
args = args.filter(function (arg) {
Expand All @@ -102,8 +105,7 @@ function printUsage() {
switch (arg) {
case 'v':
case 'version':
console.log("lessc " + less.version.join('.') + " (Less Compiler) [JavaScript]");
continueProcessing = false;
exitWithLog("lessc " + less.version.join('.') + " (Less Compiler) [JavaScript]");
break;
case 'verbose':
verbose = true;
Expand Down Expand Up @@ -250,29 +252,23 @@ function printUsage() {
if (plugin) {
plugins.push(plugin);
} else {
console.error("Unable to load plugin " + name +
exitWithError("Unable to load plugin " + name +
" please make sure that it is installed under or at the same level as less");
currentErrorcode = 1;
}
break;
default:
plugin = pluginLoader.tryLoadPlugin("less-plugin-" + arg, match[2]);
if (plugin) {
plugins.push(plugin);
} else {
console.error("Unable to interpret argument " + arg +
exitWithError("Unable to interpret argument " + arg +
" - if it is a plugin (less-plugin-" + arg + "), make sure that it is installed under or at" +
" the same level as less");
currentErrorcode = 1;
}
break;
}
});

if (!continueProcessing) {
return;
}

var input = args[1];
if (input && input != '-') {
input = path.resolve(process.cwd(), input);
Expand Down Expand Up @@ -325,7 +321,7 @@ function printUsage() {
console.error("lessc: no input files");
console.error("");
printUsage();
currentErrorcode = 1;
process.exit(1);
return;
}

Expand Down Expand Up @@ -420,8 +416,7 @@ function printUsage() {

var parseLessFile = function (e, data) {
if (e) {
console.error("lessc: " + e.message);
currentErrorcode = 1;
exitWithError("lessc: " + e.message);
return;
}

Expand Down Expand Up @@ -468,7 +463,7 @@ function printUsage() {
},
function(err) {
less.writeError(err, options);
currentErrorcode = 1;
process.exit(1);
});
};

Expand Down

0 comments on commit 0bbb1c5

Please sign in to comment.