src/content/docs/linter/rules/use-throw-only-error.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.8.0` - Diagnostic Category: [`lint/style/useThrowOnlyError`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Inspired from [`no-throw-literal`](https://eslint.org/docs/latest/rules/no-throw-literal) - Inspired from [`@typescript-eslint/only-throw-error`](https://typescript-eslint.io/rules/only-throw-error){
"linter": {
"rules": {
"style": {
"useThrowOnlyError": "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.
throw undefined;
throw false;
throw "a" + "b";
throw new Error();
throw new TypeError('biome');
class CustomError extends Error {}
throw new CustomError();
This 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.