Skip to content

Commit 160b8f7

Browse files
committed
[FEATURE] CLI: Improve error reporting
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.
1 parent 0394f1a commit 160b8f7

File tree

3 files changed

+57
-5
lines changed

3 files changed

+57
-5
lines changed

lib/cli/commands/base.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,58 @@ cli.usage("Usage: ui5 <command> [options]")
2929
.example("ui5 <command> --translator static:/path/to/projectDependencies.yaml",
3030
"Execute command using a \"static\" translator with translator parameters")
3131
.example("ui5 <command> --config /path/to/ui5.yaml",
32-
"Execute command using a project configuration from custom path");
32+
"Execute command using a project configuration from custom path")
33+
.fail(function(msg, err, yargs) {
34+
const chalk = require("chalk");
35+
if (err) {
36+
// Exception
37+
const logger = require("@ui5/logger");
38+
if (logger.isLevelEnabled("error")) {
39+
console.log("");
40+
console.log(chalk.bold.red("⚠️ Process Failed With Error"));
41+
42+
console.log("");
43+
console.log(chalk.underline("Error Message:"));
44+
console.log(err.message);
45+
46+
if (logger.isLevelEnabled("verbose")) {
47+
console.log("");
48+
console.log(chalk.underline("Stack Trace:"));
49+
console.log(err.stack);
50+
51+
// Try to guess responsible module from stack trace file paths
52+
const moduleRegExp = /@?ui5.(?:logger|fs|builder|server|project|cli)/ig;
53+
54+
// Only check the lowest stack entry
55+
const rootStackEntry = err.stack.split("\n")[1];
56+
const match = rootStackEntry.match(moduleRegExp);
57+
if (match) {
58+
// Use the last match of the line because of cases like this:
59+
// node_modules/@ui5/cli/node_modules/@ui5/builder/lib/ => should match the builder
60+
let moduleNameGuess = match[match.length - 1];
61+
62+
// Normalize match
63+
moduleNameGuess = moduleNameGuess.replace(/.*(ui5).(.*)/i, "$1-$2").toLowerCase();
64+
const newIssueUrl = `https://github.com/SAP/${moduleNameGuess}/issues/new`;
65+
console.log("");
66+
console.log(
67+
chalk.dim(
68+
`If you think this is an issue of the UI5 Tooling, you might report it using the ` +
69+
`following URL: `) +
70+
chalk.dim.bold.underline(newIssueUrl));
71+
}
72+
} else {
73+
console.log("");
74+
console.log(chalk.dim(
75+
`For details, execute the same command again with an additional '--verbose' parameter`));
76+
}
77+
}
78+
} else {
79+
// Yargs error
80+
console.log(chalk.bold.yellow("Command Failed:"));
81+
console.log(`${msg}`);
82+
console.log("");
83+
console.log(chalk.dim(`See 'ui5 --help' or 'ui5 build --help' for help`));
84+
}
85+
process.exit(1);
86+
});

lib/cli/commands/build.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ function handleBuild(argv) {
7878
const command = argv._[argv._.length - 1];
7979
logger.setShowProgress(true);
8080

81-
normalizer.generateProjectTree({
81+
return normalizer.generateProjectTree({
8282
translatorName: argv.translator,
8383
configPath: argv.config
8484
}).then(function(tree) {
@@ -94,9 +94,6 @@ function handleBuild(argv) {
9494
includedTasks: argv["include-task"],
9595
excludedTasks: argv["exclude-task"]
9696
});
97-
}).catch(function(err) {
98-
console.error(err);
99-
process.exit(1);
10097
});
10198
}
10299

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
"@ui5/logger": "^1.0.2",
107107
"@ui5/project": "^1.1.0",
108108
"@ui5/server": "^1.3.0",
109+
"chalk": "^2.4.2",
109110
"import-local": "^3.0.2",
110111
"js-yaml": "^3.13.1",
111112
"open": "^6.4.0",

0 commit comments

Comments
 (0)