Linter · Rules
no-useless-catch
Disallow catch clauses that only rethrow the error
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
Reported:
try {
await save(user);
} catch (error) {
throw error;
}
Not reported:
await save(user);
With a finally, only the catch clause is unnecessary:
try {
await save(user);
} catch (error) {
throw error;
} finally {
unlock(user);
}
try {
await save(user);
} finally {
unlock(user);
}
A catch that does something first, or throws a different error, is fine:
try {
await save(user);
} catch (error) {
log(error);
throw error;
}
try {
await save(user);
} catch (error) {
throw new Error("could not save", { cause: error });
}