Linter · Rules
no-ex-assign
Disallow assigning to the error parameter of a catch clause
Assigning to a catch clause's parameter overwrites the error that was caught. From that line on, nothing in the block can read the original message or stack.
This rule is on by default.
Configure
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"no-ex-assign" = "off" # or "error"
Examples
Reported:
try {
upload(file);
} catch (error) {
error = new Error("Upload failed");
report(error);
}
Names that the parameter destructures count as well:
try {
upload(file);
} catch ({ message }) {
message = "Upload failed";
report(message);
}
Not reported:
try {
upload(file);
} catch (error) {
const wrapped = new Error("Upload failed", { cause: error });
report(wrapped);
}
Setting a property keeps the error, so it's allowed:
try {
upload(file);
} catch (error) {
error.file = file;
report(error);
}