useThrowOnlyError
此内容尚不支持你的语言。
Diagnostic Category: lint/style/useThrowOnlyError
Since: v1.8.0
Sources:
- Inspired from:
no-throw-literal
- Inspired from:
@typescript-eslint/only-throw-error
Disallow 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 Invalidcode-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.
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.
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 ValidCaveats
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.