Skip to content

Commit 39f7fb4

Browse files
authored
feat: preserve-caught-error should recognize all static "cause" keys (#20163)
1 parent f81eabc commit 39f7fb4

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

lib/rules/preserve-caught-error.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,7 @@ function getErrorCause(throwStatement) {
8282
}
8383

8484
const causeProperties = errorOptions.properties.filter(
85-
prop =>
86-
prop.type === "Property" &&
87-
prop.key.type === "Identifier" &&
88-
prop.key.name === "cause" &&
89-
!prop.computed, // It is hard to accurately identify the value of computed props
85+
prop => astUtils.getStaticPropertyName(prop) === "cause",
9086
);
9187

9288
const causeProperty = causeProperties.at(-1);

tests/lib/rules/preserve-caught-error.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@ ruleTester.run("preserve-caught-error", rule, {
2323
} catch (error) {
2424
throw new Error("Failed to perform error prone operations", { cause: error });
2525
}`,
26+
`try {
27+
doSomething();
28+
} catch (error) {
29+
throw new Error("Something failed", { 'cause': error });
30+
}`,
31+
`try {
32+
doSomething();
33+
} catch (error) {
34+
throw new Error("Something failed", { "cause": error });
35+
}`,
36+
`try {
37+
doSomething();
38+
} catch (error) {
39+
throw new Error("Something failed", { ['cause']: error });
40+
}`,
41+
`try {
42+
doSomething();
43+
} catch (error) {
44+
throw new Error("Something failed", { ["cause"]: error });
45+
}`,
46+
`try {
47+
doSomething();
48+
} catch (error) {
49+
throw new Error("Something failed", { [\`cause\`]: error });
50+
}`,
2651
/* No throw inside catch */
2752
`try {
2853
doSomething();
@@ -92,6 +117,16 @@ ruleTester.run("preserve-caught-error", rule, {
92117
} catch (error) {
93118
throw new Error("Something failed", { cause: anotherError, cause: error });
94119
}`,
120+
`try {
121+
doSomething();
122+
} catch (error) {
123+
throw new Error("Something failed", { "cause": anotherError, "cause": error });
124+
}`,
125+
`try {
126+
doSomething();
127+
} catch (error) {
128+
throw new Error("Something failed", { cause: anotherError, "cause": error });
129+
}`,
95130
],
96131
invalid: [
97132
/* 1. Throws a new Error without cause, even though an error was caught */
@@ -136,6 +171,30 @@ ruleTester.run("preserve-caught-error", rule, {
136171
} catch (err) {
137172
const unrelated = new Error("other");
138173
throw new Error("Something failed", { cause: err });
174+
}`,
175+
},
176+
],
177+
},
178+
],
179+
},
180+
{
181+
code: `try {
182+
doSomething();
183+
} catch (err) {
184+
const unrelated = new Error("other");
185+
throw new Error("Something failed", { "cause": unrelated });
186+
}`,
187+
errors: [
188+
{
189+
messageId: "incorrectCause",
190+
suggestions: [
191+
{
192+
messageId: "includeCause",
193+
output: `try {
194+
doSomething();
195+
} catch (err) {
196+
const unrelated = new Error("other");
197+
throw new Error("Something failed", { "cause": err });
139198
}`,
140199
},
141200
],
@@ -735,6 +794,12 @@ ruleTester.run("preserve-caught-error", rule, {
735794
}`,
736795
errors: [{ messageId: "incorrectCause" }],
737796
},
797+
{
798+
code: `try {} catch (error) {
799+
throw new Error("Something failed", { cause: error, "cause": anotherError });
800+
}`,
801+
errors: [{ messageId: "incorrectCause" }],
802+
},
738803
/* 26. Getters and setters as `cause`. */
739804
{
740805
code: `try {} catch (error) {

0 commit comments

Comments
 (0)