useThrowOnlyError
Este conteúdo não está disponível em sua língua ainda.
Diagnostic Category: lint/style/useThrowOnlyError
Since: v1.8.0
Sources:
- Inspired from:
no-throw-literal
- Inspired from:
@typescript-eslint/only-throw-error
Description
Section titled DescriptionDisallow throwing non-Error
values.
It is considered good practice only to throw the Error
object itself or an object using the Error
object
as base objects for user-defined exceptions. The fundamental benefit of Error
objects is that they automatically
keep track of where they were built and originated.
Examples
Section titled ExamplesInvalid
Section titled Invalidthrow undefined;
code-block.js:1:1 lint/style/useThrowOnlyError ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Throwing non-Error values is not allowed.
> 1 │ throw undefined;
│ ^^^^^^^^^^^^^^^^
2 │
ℹ While Javascript supports throwing any value, handling non-Error values is confusing.
throw false;
code-block.js:1:1 lint/style/useThrowOnlyError ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Throwing non-Error values is not allowed.
> 1 │ throw false;
│ ^^^^^^^^^^^^
2 │
ℹ While Javascript supports throwing any value, handling non-Error values is confusing.
throw "a" + "b";
code-block.js:1:1 lint/style/useThrowOnlyError ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ Throwing non-Error values is not allowed.
> 1 │ throw “a” + “b”;
│ ^^^^^^^^^^^^^^^^
2 │
ℹ While Javascript supports throwing any value, handling non-Error values is confusing.
Valid
Section titled Validthrow new Error();
throw new TypeError('biome');
class CustomError extends Error {}
throw new CustomError();
Caveats
Section titled CaveatsThis rule only covers cases where throwing the value can be known statically. Complex cases such as object and function access aren’t checked. This will be improved in the future once Biome supports type inference.
How to configure
Section titled How to configure{ "linter": { "rules": { "style": { "useThrowOnlyError": "error" } } }}