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

1480 lines
32 KiB
JavaScript

/**
* @fileoverview Rule to preserve caught errors when re-throwing exceptions
* @author Amnish Singh Arora
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const rule = require("../../../lib/rules/preserve-caught-error"),
RuleTester = require("../../../lib/rule-tester/rule-tester");
//------------------------------------------------------------------------------
// Tests
//------------------------------------------------------------------------------
const ruleTester = new RuleTester();
ruleTester.run("preserve-caught-error", rule, {
valid: [
`try {
throw new Error("Original error");
} catch (error) {
throw new Error("Failed to perform error prone operations", { cause: error });
}`,
`try {
doSomething();
} catch (error) {
throw new Error("Something failed", { 'cause': error });
}`,
`try {
doSomething();
} catch (error) {
throw new Error("Something failed", { "cause": error });
}`,
`try {
doSomething();
} catch (error) {
throw new Error("Something failed", { ['cause']: error });
}`,
`try {
doSomething();
} catch (error) {
throw new Error("Something failed", { ["cause"]: error });
}`,
`try {
doSomething();
} catch (error) {
throw new Error("Something failed", { [\`cause\`]: error });
}`,
/* No throw inside catch */
`try {
doSomething();
} catch (e) {
console.error(e);
}`,
`try {
doSomething();
} catch (err) {
throw new Error("Failed", { cause: err, extra: 42 });
}`,
`try {
doSomething();
} catch (error) {
switch (error.code) {
case "A":
throw new Error("Type A", { cause: error });
case "B":
throw new Error("Type B", { cause: error });
default:
throw new Error("Other", { cause: error });
}
}`,
/* When the error options are too complicated to be properly analyzed/fixed */
`try {
// ...
} catch (err) {
const opts = { cause: err }
throw new Error("msg", { ...opts });
}
`,
/* When the thrown error is part of a function defined in catch block, the caught error is not directly related to that throw */
`try {
} catch (error) {
foo = {
bar() {
throw new Error();
}
};
}`,
/* 19. When there is a `SpreadStatement` argument passed to the Error constructor, we can't provide an accurate suggestion. */
`try {
doSomething();
} catch (error) {
const args = [];
throw new Error(...args);
}`,
/* Do not report instances where thrown error is an instance of a custom error type that shadows built-in class. */
`import { Error } from "./my-custom-error.js";
try {
doSomething();
} catch (error) {
throw Error("Failed to perform error prone operations");
}`,
/* It's valid to discard the caught error at parameter level of catch block `requireCatchParameter` is set to `false` (default behavior) */
{
code: `try {
doSomething();
} catch {
throw new Error("Something went wrong");
}`,
options: [{ requireCatchParameter: false }],
},
/* Multiple cause properties are present and the last one is the expected caught error value. */
`try {
doSomething();
} catch (error) {
throw new Error("Something failed", { cause: anotherError, cause: error });
}`,
`try {
doSomething();
} catch (error) {
throw new Error("Something failed", { "cause": anotherError, "cause": error });
}`,
`try {
doSomething();
} catch (error) {
throw new Error("Something failed", { cause: anotherError, "cause": error });
}`,
// MemberExpression - Custom errors accessed via namespace
{
code: `
const errors = { AppError: class AppError extends Error {} };
try {
doSomething();
} catch (error) {
throw new errors.AppError("Something failed", { cause: error });
}`,
options: [{ errorClassNames: ["AppError"] }],
},
{
code: `
const lib = { APIError: class APIError extends Error {} };
try {
doSomething();
} catch (error) {
throw new lib.APIError("Something failed", { cause: error });
}`,
options: [{ errorClassNames: ["APIError"] }],
},
// Custom error class names - valid (Identifier with correct cause)
{
code: `
class CustomApplicationError extends Error {}
try {
doSomething();
} catch (err) {
throw new CustomApplicationError("Cause not provided", { cause: err });
}`,
options: [{ errorClassNames: ["CustomApplicationError"] }],
},
{
code: `
class APIError extends Error {}
try {
doSomething();
} catch (error) {
throw new APIError("API failed", { cause: error });
}`,
options: [{ errorClassNames: ["APIError"] }],
},
/* Custom error not in errorClassNames list — rule should not report */
{
code: `
class CustomError extends Error {}
try {
doSomething();
} catch (err) {
throw new CustomError("No cause");
}`,
options: [{ errorClassNames: [] }],
},
// argumentPosition - custom error with options at position 3
{
code: `
try {
doSomething();
} catch (err) {
throw new MyError("failed", someData, { cause: err });
}`,
options: [
{ errorClassNames: [{ name: "MyError", argumentPosition: 3 }] },
],
},
// argumentPosition - custom error with options at position 1
{
code: `
try {
doSomething();
} catch (err) {
throw new SimpleError({ cause: err });
}`,
options: [
{
errorClassNames: [
{ name: "SimpleError", argumentPosition: 1 },
],
},
],
},
// Duplicate configurations for the same class name apply "last-in wins"
{
code: `
try {
doSomething();
} catch (err) {
throw new AppError("Message", {}, { cause: err });
}`,
options: [
{
errorClassNames: [
{ name: "AppError", argumentPosition: 2 },
{ name: "AppError", argumentPosition: 3 },
],
},
],
},
{
code: `
try {
doSomething();
} catch (err) {
throw new context.Error("failed", someData, { cause: err });
}`,
options: [
{
errorClassNames: [{ name: "Error", argumentPosition: 3 }],
},
],
},
{
code: `
try {
doSomething();
} catch (err) {
throw new Error("failed", { cause: err });
}`,
options: [
{
errorClassNames: [{ name: "Error", argumentPosition: 3 }],
},
],
},
{
code: `import { AggregateError } from "some-module";
try {
doSomething();
} catch (err) {
throw new AggregateError({ cause: err });
}`,
options: [
{
errorClassNames: [
{ name: "AggregateError", argumentPosition: 1 },
],
},
],
},
],
invalid: [
/* 1. Throws a new Error without cause, even though an error was caught */
{
code: `try {
doSomething();
} catch (err) {
throw new Error("Something failed");
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
throw new Error("Something failed", { cause: err });
}`,
},
],
},
],
},
/* 2. Throwing a new Error with unrelated cause */
{
code: `try {
doSomething();
} catch (err) {
const unrelated = new Error("other");
throw new Error("Something failed", { cause: unrelated });
}`,
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
const unrelated = new Error("other");
throw new Error("Something failed", { cause: err });
}`,
},
],
},
],
},
{
code: `try {
doSomething();
} catch (err) {
const unrelated = new Error("other");
throw new Error("Something failed", { "cause": unrelated });
}`,
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
const unrelated = new Error("other");
throw new Error("Something failed", { "cause": err });
}`,
},
],
},
],
},
/* 3. Throws a new Error, cause property is present but value is a different identifier */
/* Note: This should actually be a valid case since e === err, but still reporting as it's hard to track. */
{
code: `try {
doSomething();
} catch (err) {
const e = err;
throw new Error("Failed", { cause: e });
}`,
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
const e = err;
throw new Error("Failed", { cause: err });
}`,
},
],
},
],
},
/* 4. Throws a new Error, but not using the full caught error as the cause of the symptom error */
{
code: `try {
doSomething();
} catch (error) {
throw new Error("Failed", { cause: error.message });
}`,
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
throw new Error("Failed", { cause: error });
}`,
},
],
},
],
},
/* 5. Throw in a heavily nested catch block */
{
code: `try {
doSomething();
} catch (error) {
if (shouldThrow) {
while (true) {
if (Math.random() > 0.5) {
throw new Error("Failed without cause");
}
}
}
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
if (shouldThrow) {
while (true) {
if (Math.random() > 0.5) {
throw new Error("Failed without cause", { cause: error });
}
}
}
}`,
},
],
},
],
},
/* 6. Throw deep inside a switch statement */
{
code: `try {
doSomething();
} catch (error) {
switch (error.code) {
case "A":
throw new Error("Type A");
case "B":
throw new Error("Type B", { cause: error });
default:
throw new Error("Other", { cause: error });
}
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
switch (error.code) {
case "A":
throw new Error("Type A", { cause: error });
case "B":
throw new Error("Type B", { cause: error });
default:
throw new Error("Other", { cause: error });
}
}`,
},
],
},
],
},
/* 7. Throw statement with a template literal error message */
{
code: `try {
doSomething();
} catch (error) {
throw new Error(\`The certificate key "\${chalk.yellow(keyFile)}" is invalid.\n\${err.message}\`);
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
throw new Error(\`The certificate key "\${chalk.yellow(keyFile)}" is invalid.\n\${err.message}\`, { cause: error });
}`,
},
],
},
],
},
/* 8. Throw statement with a variable error message */
{
code: `try {
doSomething();
} catch (error) {
const errorMessage = "Operation failed";
throw new Error(errorMessage);
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
const errorMessage = "Operation failed";
throw new Error(errorMessage, { cause: error });
}`,
},
],
},
],
},
/* 9. Existing error options should be preserved. */
{
code: `try {
doSomething();
} catch (error) {
const errorMessage = "Operation failed";
throw new Error(errorMessage, { existingOption: true, complexOption: { moreOptions: {} } });
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
const errorMessage = "Operation failed";
throw new Error(errorMessage, { existingOption: true, complexOption: { moreOptions: {} }, cause: error });
}`,
},
],
},
],
},
/* 10. Multiple Throw statements within a single catch block */
{
code: `try {
doSomething();
} catch (err) {
if (err.code === "A") {
throw new Error("Type A");
}
throw new TypeError("Fallback error");
}`,
// This should have multiple errors
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
if (err.code === "A") {
throw new Error("Type A", { cause: err });
}
throw new TypeError("Fallback error");
}`,
},
],
},
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
if (err.code === "A") {
throw new Error("Type A");
}
throw new TypeError("Fallback error", { cause: err });
}`,
},
],
},
],
},
/* 11. When an Error is created without `new` keyword */
{
code: `try {
doSomething();
} catch (err) {
throw Error("Something failed");
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
throw Error("Something failed", { cause: err });
}`,
},
],
},
],
},
/* 12. Miscellaneous constructs */
{
code: `try {
} catch (err) {
my_label:
throw new Error("Failed without cause");
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
} catch (err) {
my_label:
throw new Error("Failed without cause", { cause: err });
}`,
},
],
},
],
},
{
code: `try {
} catch (err) {
{
throw new Error("Something went wrong");
}
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
} catch (err) {
{
throw new Error("Something went wrong", { cause: err });
}
}`,
},
],
},
],
},
/* 13. When the throw Error constructor has no message argument. */
{
code: `try {
} catch (err) {
{
throw new Error();
}
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
} catch (err) {
{
throw new Error("", { cause: err });
}
}`,
},
],
},
],
},
/* 14. AggregateError accepts options as the third argument. */
{
code: `try {
} catch (err) {
{
throw new AggregateError([], "Lorem ipsum");
}
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
} catch (err) {
{
throw new AggregateError([], "Lorem ipsum", { cause: err });
}
}`,
},
],
},
],
},
/* 15. `AggregateError` with no arguments. */
{
code: `try {
} catch (err) {
{
throw new AggregateError();
}
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
} catch (err) {
{
throw new AggregateError([], "", { cause: err });
}
}`,
},
],
},
],
},
/* 16. `AggregateError` with just `errors` argument. */
{
code: `try {
} catch (err) {
{
throw new AggregateError([]);
}
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
} catch (err) {
{
throw new AggregateError([], "", { cause: err });
}
}`,
},
],
},
],
},
/* 17. Disallow discarding caught errors when `requireCatchParameter` is set to `true` */
{
code: `try {
doSomething();
} catch {
throw new Error("Something went wrong");
}`,
options: [{ requireCatchParameter: true }],
errors: [
{
messageId: "missingCatchErrorParam",
},
],
},
/* 18. Throwing a new Error with unrelated cause, and complex fix is needed. */
{
code: `try {
doSomething();
} catch (err) {
throw new Error("Something failed", { cause });
}`,
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
throw new Error("Something failed", { cause: err });
}`,
},
],
},
],
},
/* 19. When the caught error is being partially lost. */
{
code: `try {
doSomething();
} catch ({ message }) {
throw new Error(message);
}`,
errors: [
{
messageId: "partiallyLostError",
},
],
},
{
code: `try {
doSomethingElse();
} catch ({ ...error }) {
throw new Error(error.message);
}`,
errors: [
{
messageId: "partiallyLostError",
},
],
},
/* 20. When the caught error is shadowed by a closer scoped redeclaration. */
{
code: `try {
doSomething();
} catch (error) {
if (whatever) {
const error = anotherError;
throw new Error("Something went wrong", { cause: error });
}
}`,
errors: [
{
messageId: "caughtErrorShadowed",
},
],
},
/* 21. Make sure comments are preserved when fixing missing cause. */
{
code: `try {
doSomething();
} catch (error) {
throw new Error(
"Something went wrong" // some comments
);
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
throw new Error(
"Something went wrong", { cause: error } // some comments
);
}`,
},
],
},
],
},
/* 22. Adding `cause` to an empty existing options object. */
{
code: `try {
doSomething();
} catch (err) {
throw new Error("Something failed", {});
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (err) {
throw new Error("Something failed", {cause: err});
}`,
},
],
},
],
},
/* 23. There is no easy way to check for `cause` existence when property is computed. */
{
code: `try {
doSomething();
} catch (error) {
const cause = "desc";
throw new Error("Something failed", { [cause]: "Some error" });
}`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
const cause = "desc";
throw new Error("Something failed", { [cause]: "Some error", cause: error });
}`,
},
],
},
],
},
/* 24. When an incorrect cause is attached as a shorthand method. */
{
code: `try {
doSomething();
} catch (error) {
throw new Error("Something failed", { cause() { /* do something */ } });
}`,
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `try {
doSomething();
} catch (error) {
throw new Error("Something failed", { cause: error });
}`,
},
],
},
],
},
/* 25. When multiple `cause` properties are present. */
{
code: `try {} catch (error) {
throw new Error("Something failed", { cause: error, cause: anotherError });
}`,
errors: [{ messageId: "incorrectCause" }],
},
{
code: `try {} catch (error) {
throw new Error("Something failed", { cause: error, "cause": anotherError });
}`,
errors: [{ messageId: "incorrectCause" }],
},
/* 26. Getters and setters as `cause`. */
{
code: `try {} catch (error) {
throw new Error("Something failed", { get cause() { } });
}`,
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `try {} catch (error) {
throw new Error("Something failed", { cause: error });
}`,
},
],
},
],
},
{
code: `try {} catch (error) {
throw new Error("Something failed", { set cause(value) { } });
}`,
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `try {} catch (error) {
throw new Error("Something failed", { cause: error });
}`,
},
],
},
],
},
{
code: `try {} catch (error) {
throw new Error("Something failed", {
get cause() { return error; },
set cause(value) { error = value; },
});
}`,
errors: [{ messageId: "incorrectCause" }],
},
/* Parenthesized arguments: insertions must land outside the wrapping parentheses */
{
code: `try { doSomething(); } catch (err) { throw new Error(("Something failed")); }`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try { doSomething(); } catch (err) { throw new Error(("Something failed"), { cause: err }); }`,
},
],
},
],
},
{
code: `try { doSomething(); } catch (err) { throw new Error(("Something failed"),); }`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try { doSomething(); } catch (err) { throw new Error(("Something failed"), { cause: err },); }`,
},
],
},
],
},
{
code: `try { doSomething(); } catch (err) { throw new AggregateError((errors)); }`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try { doSomething(); } catch (err) { throw new AggregateError((errors), "", { cause: err }); }`,
},
],
},
],
},
{
code: `try { doSomething(); } catch (err) { throw new AggregateError(errors, ("message")); }`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try { doSomething(); } catch (err) { throw new AggregateError(errors, ("message"), { cause: err }); }`,
},
],
},
],
},
{
code: `try { doSomething(); } catch (err) { throw new CustomError((foo)); }`,
options: [{ errorClassNames: ["CustomError"] }],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try { doSomething(); } catch (err) { throw new CustomError((foo), { cause: err }); }`,
},
],
},
],
},
// Custom error class names - missing cause (Identifier)
{
code: `
class CustomApplicationError extends Error {}
try {
doSomething();
} catch (err) {
throw new CustomApplicationError("Cause not provided");
}`,
options: [{ errorClassNames: ["CustomApplicationError"] }],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `
class CustomApplicationError extends Error {}
try {
doSomething();
} catch (err) {
throw new CustomApplicationError("Cause not provided", { cause: err });
}`,
},
],
},
],
},
{
code: `
class APIError extends Error {}
try {
doSomething();
} catch (error) {
throw new APIError("API failed", { cause: wrong });
}`,
options: [{ errorClassNames: ["APIError"] }],
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `
class APIError extends Error {}
try {
doSomething();
} catch (error) {
throw new APIError("API failed", { cause: error });
}`,
},
],
},
],
},
// MemberExpression - Custom errors missing cause
{
code: `
const errors = { AppError: class AppError extends Error {} };
try {
doSomething();
} catch (error) {
throw new errors.AppError("Something failed");
}`,
options: [{ errorClassNames: ["AppError"] }],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `
const errors = { AppError: class AppError extends Error {} };
try {
doSomething();
} catch (error) {
throw new errors.AppError("Something failed", { cause: error });
}`,
},
],
},
],
},
// MemberExpression - Custom errors with incorrect cause
{
code: `
const lib = { APIError: class APIError extends Error {} };
try {
doSomething();
} catch (error) {
throw new lib.APIError("Something failed", { cause: wrong });
}`,
options: [{ errorClassNames: ["APIError"] }],
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `
const lib = { APIError: class APIError extends Error {} };
try {
doSomething();
} catch (error) {
throw new lib.APIError("Something failed", { cause: error });
}`,
},
],
},
],
},
// argumentPosition - missing cause at position 3
{
code: `
try {
doSomething();
} catch (err) {
throw new MyError("failed", someData);
}`,
options: [
{ errorClassNames: [{ name: "MyError", argumentPosition: 3 }] },
],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `
try {
doSomething();
} catch (err) {
throw new MyError("failed", someData, { cause: err });
}`,
},
],
},
],
},
// argumentPosition - incorrect cause at position 3
{
code: `
try {
doSomething();
} catch (err) {
throw new MyError("failed", someData, { cause: wrong });
}`,
options: [
{ errorClassNames: [{ name: "MyError", argumentPosition: 3 }] },
],
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `
try {
doSomething();
} catch (err) {
throw new MyError("failed", someData, { cause: err });
}`,
},
],
},
],
},
// argumentPosition - missing cause at position 1
{
code: `
try {
doSomething();
} catch (err) {
throw new SimpleError();
}`,
options: [
{
errorClassNames: [
{ name: "SimpleError", argumentPosition: 1 },
],
},
],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `
try {
doSomething();
} catch (err) {
throw new SimpleError({ cause: err });
}`,
},
],
},
],
},
// Custom error with fewer args than the options position - no suggestion
{
code: `
try {
doSomething();
} catch (err) {
throw new MyError();
}`,
options: [{ errorClassNames: ["MyError"] }],
errors: [
{
messageId: "missingCause",
suggestions: [],
},
],
},
{
code: `
try {
doSomething();
} catch (err) {
throw new context.Error("failed", someData);
}`,
options: [
{
errorClassNames: [{ name: "Error", argumentPosition: 3 }],
},
],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `
try {
doSomething();
} catch (err) {
throw new context.Error("failed", someData, { cause: err });
}`,
},
],
},
],
},
{
code: `
try {
doSomething();
} catch (err) {
throw new Error("failed");
}`,
options: [
{
errorClassNames: [{ name: "Error", argumentPosition: 3 }],
},
],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `
try {
doSomething();
} catch (err) {
throw new Error("failed", { cause: err });
}`,
},
],
},
],
},
{
code: `import { AggregateError } from "some-module";
try {
doSomething();
} catch (err) {
throw new AggregateError();
}`,
options: [
{
errorClassNames: [
{ name: "AggregateError", argumentPosition: 1 },
],
},
],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `import { AggregateError } from "some-module";
try {
doSomething();
} catch (err) {
throw new AggregateError({ cause: err });
}`,
},
],
},
],
},
{
code: `import { AggregateError } from "some-module";
try {
doSomething();
} catch (err) {
throw new AggregateError({ cause: wrong });
}`,
options: [
{
errorClassNames: [
{ name: "AggregateError", argumentPosition: 1 },
],
},
],
errors: [
{
messageId: "incorrectCause",
suggestions: [
{
messageId: "includeCause",
output: `import { AggregateError } from "some-module";
try {
doSomething();
} catch (err) {
throw new AggregateError({ cause: err });
}`,
},
],
},
],
},
/* Built-in error constructor without argument list */
{
code: `try {} catch (err) { throw new Error; }`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {} catch (err) { throw new Error("", { cause: err }); }`,
},
],
},
],
},
/* AggregateError without argument list */
{
code: `try {} catch (err) { throw new AggregateError; }`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {} catch (err) { throw new AggregateError([], "", { cause: err }); }`,
},
],
},
],
},
/* Custom error type without argument list */
{
code: `try {} catch (err) { throw new CustomError; }`,
options: [
{
errorClassNames: [
{ name: "CustomError", argumentPosition: 1 },
],
},
],
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {} catch (err) { throw new CustomError({ cause: err }); }`,
},
],
},
],
},
/* Parenthesized error constructor without argument list */
{
code: `try {} catch (err) { throw new (Error); }`,
errors: [
{
messageId: "missingCause",
suggestions: [
{
messageId: "includeCause",
output: `try {} catch (err) { throw new (Error)("", { cause: err }); }`,
},
],
},
],
},
],
});