Skip to content

Commit 5301dec

Browse files
feat: show all cause in Error (#5422)
* 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>
1 parent 8521d29 commit 5301dec

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

cli/logging.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,23 @@ export function handleError(error: RollupError, recover = false): void {
3535
outputLines.push(dim(error.stack?.replace(`${nameSection}${error.message}\n`, '')));
3636
}
3737

38+
// ES2022: Error.prototype.cause is optional
39+
if (error.cause) {
40+
let cause = error.cause as Error | undefined;
41+
const causeErrorLines = [];
42+
let indent = '';
43+
44+
while (cause) {
45+
indent += ' ';
46+
const message = cause.stack || cause;
47+
causeErrorLines.push(...`[cause] ${message}`.split('\n').map(line => indent + line));
48+
49+
cause = cause.cause as Error | undefined;
50+
}
51+
52+
outputLines.push(dim(causeErrorLines.join('\n')));
53+
}
54+
3855
outputLines.push('', '');
3956
stderr(outputLines.join('\n'));
4057

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { assertIncludes } = require('../../../utils.js');
2+
3+
module.exports = defineTest({
4+
description: 'prints error cause',
5+
command: 'rollup --config rollup.config.mjs',
6+
// We expect an error and want to make assertions about the output
7+
error: () => true,
8+
stderr: stderr => {
9+
// We just assert the parts of the output that do not change
10+
assertIncludes(stderr, '\n[!] (plugin at position 1) Error: Outer error\n at ');
11+
assertIncludes(stderr, '\n [cause] Error: Inner error\n at ');
12+
assertIncludes(stderr, '\n [cause] Error: Innermost error\n at ');
13+
}
14+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
assert.ok(true);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default {
2+
input: "main.js",
3+
plugins: [
4+
{
5+
buildStart() {
6+
throw new Error("Outer error", {
7+
cause: new Error("Inner error", {
8+
cause: new Error("Innermost error")
9+
})
10+
});
11+
}
12+
}
13+
]
14+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy