noMissingThrow (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noMissingThrow - This rule has an unsafe fix.
- The default severity of this rule is error.
- Sources:
- Inspired from
oxc/missing-throw
- Inspired from
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noMissingThrow": "error" } } }}Description
Section titled “Description”Disallow creating an error with new without throwing it.
Writing new Error() as a statement, without using its result, creates
an error object and then discards it. Nothing happens: the code keeps running as if no error
occurred. This is almost always a mistake where the throw keyword was
forgotten.
The rule checks constructors whose name starts with an uppercase letter
and ends with Error, such as Error, TypeError, or ValidationError.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”function check(value) { if (!value) { new Error("Missing value"); }}code-block.js:3:9 lint/nursery/noMissingThrow FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This error is created but never thrown.
1 │ function check(value) {
2 │ if (!value) {
> 3 │ new Error("Missing value");
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
4 │ }
5 │ }
ℹ Creating an error does nothing by itself, so the code keeps running as if nothing went wrong.
ℹ Add the throw keyword to throw the error, or remove the expression if it isn't needed.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Add the throw keyword.
3 │ ········throw·new·Error("Missing·value");
│ ++++++
const fail = () => { new TypeError("Unexpected type");};code-block.js:2:5 lint/nursery/noMissingThrow FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This error is created but never thrown.
1 │ const fail = () => {
> 2 │ new TypeError("Unexpected type");
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3 │ };
4 │
ℹ Creating an error does nothing by itself, so the code keeps running as if nothing went wrong.
ℹ Add the throw keyword to throw the error, or remove the expression if it isn't needed.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Add the throw keyword.
2 │ ····throw·new·TypeError("Unexpected·type");
│ ++++++
function check(value) { if (!value) { throw new Error("Missing value"); }}
const createError = () => new Error("Something went wrong");
const error = new Error("Something went wrong");
reject(new Error("Something went wrong"));Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.