A catch clause only throws the error it caught again

tx0/ts:useless-catchdefaultwarningsmell

What it finds

catch (e) { throw e; }: a catch clause that only throws the error it caught again changes nothing, since the error would have left the try statement anyway. It reads as if errors were handled when they are not.

Only a clause whose single statement throws its own parameter, unchanged, is reported.

From the rule's source, useless_catch.rs.

Example from the rule's tests
async function load(id: string) {  try {    return await fetch(id);  } catch (error) {    throw error;tx0 reports: this catch only throws error again  }}async function save(id: string) {  try {    return await fetch(id);  } catch (error) {    throw new Error("cannot save " + id);  } finally {    done();  }}function done() {}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:useless-catch" = "off"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:useless-catch] reason="..."

tx0 explain tx0/ts:useless-catch shows the same in the terminal. Found a false positive? Report it.