no-ex-assign

Disallow assigning to the error parameter of a catch clause

warn by default

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#

example.js1 problem
try {
  upload(file);
} catch (error) {
  error = new Error("Upload failed");
  report(error);
}

Names that the parameter destructures count as well:

example.js1 problem
try {
  upload(file);
} catch ({ message }) {
  message = "Upload failed";
  report(message);
}
example.jsNo problems
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:

example.jsNo problems
try {
  upload(file);
} catch (error) {
  error.file = file;
  report(error);
}