Skip to content

Commit

Permalink
feat: show all cause in Error (#5422)
Browse files Browse the repository at this point in the history
* feat: show all cause in Error

* test: sample to test stderr cause

* Add indentation

---------

Co-authored-by: Lukas Taegert-Atkinson <lukastaegert@users.noreply.github.com>
Co-authored-by: Lukas Taegert-Atkinson <lukas.taegert-atkinson@tngtech.com>
  • Loading branch information
3 people committed Apr 3, 2024
1 parent 8521d29 commit 5301dec
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cli/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ export function handleError(error: RollupError, recover = false): void {
outputLines.push(dim(error.stack?.replace(`${nameSection}${error.message}\n`, '')));
}

// ES2022: Error.prototype.cause is optional
if (error.cause) {
let cause = error.cause as Error | undefined;
const causeErrorLines = [];
let indent = '';

while (cause) {
indent += ' ';
const message = cause.stack || cause;
causeErrorLines.push(...`[cause] ${message}`.split('\n').map(line => indent + line));

cause = cause.cause as Error | undefined;
}

outputLines.push(dim(causeErrorLines.join('\n')));
}

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

Expand Down
14 changes: 14 additions & 0 deletions test/cli/samples/handles-errors-cause/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { assertIncludes } = require('../../../utils.js');

module.exports = defineTest({
description: 'prints error cause',
command: 'rollup --config rollup.config.mjs',
// We expect an error and want to make assertions about the output
error: () => true,
stderr: stderr => {
// We just assert the parts of the output that do not change
assertIncludes(stderr, '\n[!] (plugin at position 1) Error: Outer error\n at ');
assertIncludes(stderr, '\n [cause] Error: Inner error\n at ');
assertIncludes(stderr, '\n [cause] Error: Innermost error\n at ');
}
});
1 change: 1 addition & 0 deletions test/cli/samples/handles-errors-cause/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assert.ok(true);
14 changes: 14 additions & 0 deletions test/cli/samples/handles-errors-cause/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export default {
input: "main.js",
plugins: [
{
buildStart() {
throw new Error("Outer error", {
cause: new Error("Inner error", {
cause: new Error("Innermost error")
})
});
}
}
]
};

0 comments on commit 5301dec

Please sign in to comment.