Skip to content

Commit

Permalink
feat: simplify stack info in cli error (#4749)
Browse files Browse the repository at this point in the history
* fix: remove name and message from stack trace

* fix: check whether stack exist

* test: tweak test

* test: try some change

* fix: Write all output in one chunk

Apparently, the test fails randomly if the new file is written in response to a
log that is not the last one

Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
  • Loading branch information
3 people committed Dec 17, 2022
1 parent e315ffc commit ffab4cd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
17 changes: 10 additions & 7 deletions cli/logging.ts
Expand Up @@ -13,27 +13,30 @@ export function handleError(error: RollupError, recover = false): void {
const pluginSection = error.plugin ? `(plugin ${error.plugin}) ` : '';
const message = `${pluginSection}${nameSection}${error.message}`;

stderr(bold(red(`[!] ${bold(message.toString())}`)));
const outputLines = [bold(red(`[!] ${bold(message.toString())}`))];

if (error.url) {
stderr(cyan(error.url));
outputLines.push(cyan(error.url));
}

if (error.loc) {
stderr(`${relativeId((error.loc.file || error.id)!)} (${error.loc.line}:${error.loc.column})`);
outputLines.push(
`${relativeId((error.loc.file || error.id)!)} (${error.loc.line}:${error.loc.column})`
);
} else if (error.id) {
stderr(relativeId(error.id));
outputLines.push(relativeId(error.id));
}

if (error.frame) {
stderr(dim(error.frame));
outputLines.push(dim(error.frame));
}

if (error.stack) {
stderr(dim(error.stack));
outputLines.push(dim(error.stack?.replace(`${nameSection}${error.message}\n`, '')));
}

stderr('');
outputLines.push('', '');
stderr(outputLines.join('\n'));

// eslint-disable-next-line unicorn/no-process-exit
if (!recover) process.exit(1);
Expand Down
5 changes: 1 addition & 4 deletions test/cli/samples/custom-frame-with-pos/_config.js
Expand Up @@ -7,9 +7,6 @@ module.exports = {
stderr: stderr =>
assertIncludes(
stderr,
'[!] (plugin at position 1) Error: My error.\n' +
'main.js (1:5)\n' +
'custom code frame\n' +
'Error: My error.'
'[!] (plugin at position 1) Error: My error.\n' + 'main.js (1:5)\n' + 'custom code frame\n'
)
};
2 changes: 1 addition & 1 deletion test/cli/samples/custom-frame/_config.js
Expand Up @@ -8,7 +8,7 @@ module.exports = {
assertIncludes(
stderr,
'[!] (plugin at position 1) Error: My error.\n' +
'main.js\ncustom code frame\nError: My error.\n' +
'main.js\ncustom code frame\n' +
' at Object.'
);
assertIncludes(stderr, 'rollup.config.js:9:19');
Expand Down
2 changes: 1 addition & 1 deletion test/cli/samples/watch/bundle-error/_config.js
Expand Up @@ -16,7 +16,7 @@ module.exports = {
setTimeout(() => unlinkSync(mainFile), 300);
},
abortOnStderr(data) {
if (data.includes('Error: Unexpected token')) {
if (data.includes('[!] RollupError: Unexpected token')) {
setTimeout(() => atomicWriteFileSync(mainFile, 'export default 42;'), 500);
return false;
}
Expand Down

0 comments on commit ffab4cd

Please sign in to comment.