no-useless-catch

Disallow catch clauses that only rethrow the error

warn by default

A catch that starts by throwing the error it caught does the same thing as no catch at all. The error reaches the caller either way, and the try only adds nesting.

This rule is on by default.

Configure#

On by default as a warning. To turn it off:

bunfig.toml
[lint.rules]
"no-useless-catch" = "off" # or "error"

Examples#

example.js1 problem
try {
  await save(user);
} catch (error) {
  throw error;
}
example.jsNo problems
await save(user);

With a finally, only the catch clause is unnecessary:

example.js1 problem
try {
  await save(user);
} catch (error) {
  throw error;
} finally {
  unlock(user);
}
example.jsNo problems
try {
  await save(user);
} finally {
  unlock(user);
}

A catch that does something first, or throws a different error, is fine:

example.jsNo problems
try {
  await save(user);
} catch (error) {
  log(error);
  throw error;
}

try {
  await save(user);
} catch (error) {
  throw new Error("could not save", { cause: error });
}