Skip to content

Commit

Permalink
[FEATURE] CLI: Improve error reporting
Browse files Browse the repository at this point in the history
Yargs errors (like 'Unkown argument') do not print the command help
anymore but a hint to use 'ui5 --help'.

Exceptions are now formatted in a nice way.

On default log level only the error message is printed out and a hint to
use the --verbose flag for details.

On verbose log level, the stack trace is printed as well and a hint
featuring the URL to open a new issue for the UI5 Tooling module that
is most likely at fault based on an analysis of the first line of the
stack trace.
  • Loading branch information
RandomByte committed Oct 14, 2019
1 parent 0394f1a commit 160b8f7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
56 changes: 55 additions & 1 deletion lib/cli/commands/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,58 @@ cli.usage("Usage: ui5 <command> [options]")
.example("ui5 <command> --translator static:/path/to/projectDependencies.yaml",
"Execute command using a \"static\" translator with translator parameters")
.example("ui5 <command> --config /path/to/ui5.yaml",
"Execute command using a project configuration from custom path");
"Execute command using a project configuration from custom path")
.fail(function(msg, err, yargs) {
const chalk = require("chalk");
if (err) {
// Exception
const logger = require("@ui5/logger");
if (logger.isLevelEnabled("error")) {
console.log("");
console.log(chalk.bold.red("⚠️ Process Failed With Error"));

console.log("");
console.log(chalk.underline("Error Message:"));
console.log(err.message);

if (logger.isLevelEnabled("verbose")) {
console.log("");
console.log(chalk.underline("Stack Trace:"));
console.log(err.stack);

// Try to guess responsible module from stack trace file paths
const moduleRegExp = /@?ui5.(?:logger|fs|builder|server|project|cli)/ig;

// Only check the lowest stack entry
const rootStackEntry = err.stack.split("\n")[1];
const match = rootStackEntry.match(moduleRegExp);
if (match) {
// Use the last match of the line because of cases like this:
// node_modules/@ui5/cli/node_modules/@ui5/builder/lib/ => should match the builder
let moduleNameGuess = match[match.length - 1];

// Normalize match
moduleNameGuess = moduleNameGuess.replace(/.*(ui5).(.*)/i, "$1-$2").toLowerCase();
const newIssueUrl = `https://github.com/SAP/${moduleNameGuess}/issues/new`;
console.log("");
console.log(
chalk.dim(
`If you think this is an issue of the UI5 Tooling, you might report it using the ` +
`following URL: `) +
chalk.dim.bold.underline(newIssueUrl));
}
} else {
console.log("");
console.log(chalk.dim(
`For details, execute the same command again with an additional '--verbose' parameter`));
}
}
} else {
// Yargs error
console.log(chalk.bold.yellow("Command Failed:"));
console.log(`${msg}`);
console.log("");
console.log(chalk.dim(`See 'ui5 --help' or 'ui5 build --help' for help`));
}
process.exit(1);
});
5 changes: 1 addition & 4 deletions lib/cli/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function handleBuild(argv) {
const command = argv._[argv._.length - 1];
logger.setShowProgress(true);

normalizer.generateProjectTree({
return normalizer.generateProjectTree({
translatorName: argv.translator,
configPath: argv.config
}).then(function(tree) {
Expand All @@ -94,9 +94,6 @@ function handleBuild(argv) {
includedTasks: argv["include-task"],
excludedTasks: argv["exclude-task"]
});
}).catch(function(err) {
console.error(err);
process.exit(1);
});
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"@ui5/logger": "^1.0.2",
"@ui5/project": "^1.1.0",
"@ui5/server": "^1.3.0",
"chalk": "^2.4.2",
"import-local": "^3.0.2",
"js-yaml": "^3.13.1",
"open": "^6.4.0",
Expand Down

0 comments on commit 160b8f7

Please sign in to comment.