Skip to content

noMissingThrow (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noMissingThrow": "error"
}
}
}
}

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.

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"));