Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): deprecate no-throw-literal and add a renamed onl…
…y-throw-error (#8701) * feat(eslint-plugin): deprecate no-throw-literal and add a renamed only-throw-error * Update packages/eslint-plugin/docs/rules/no-throw-literal.mdx Co-authored-by: YeonJuan <yeonjuan93@naver.com> * Updated options snapshot * Update packages/eslint-plugin/docs/rules/no-throw-literal.mdx Co-authored-by: auvred <61150013+auvred@users.noreply.github.com> --------- Co-authored-by: YeonJuan <yeonjuan93@naver.com> Co-authored-by: auvred <61150013+auvred@users.noreply.github.com>
- Loading branch information
1 parent
2a62a40
commit 029fc0b
Showing
15 changed files
with
743 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
description: 'Disallow throwing non-`Error` values as exceptions.' | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
import TabItem from '@theme/TabItem'; | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/only-throw-error** for documentation. | ||
It is considered good practice to only `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. | ||
|
||
This rule restricts what can be thrown as an exception. | ||
|
||
## Examples | ||
|
||
This rule is aimed at maintaining consistency when throwing exception by disallowing to throw literals and other expressions which cannot possibly be an `Error` object. | ||
|
||
<Tabs> | ||
<TabItem value="❌ Incorrect"> | ||
|
||
```ts | ||
throw 'error'; | ||
|
||
throw 0; | ||
|
||
throw undefined; | ||
|
||
throw null; | ||
|
||
const err = new Error(); | ||
throw 'an ' + err; | ||
|
||
const err = new Error(); | ||
throw `${err}`; | ||
|
||
const err = ''; | ||
throw err; | ||
|
||
function err() { | ||
return ''; | ||
} | ||
throw err(); | ||
|
||
const foo = { | ||
bar: '', | ||
}; | ||
throw foo.bar; | ||
``` | ||
|
||
</TabItem> | ||
<TabItem value="✅ Correct"> | ||
|
||
```ts | ||
throw new Error(); | ||
|
||
throw new Error('error'); | ||
|
||
const e = new Error('error'); | ||
throw e; | ||
|
||
try { | ||
throw new Error('error'); | ||
} catch (e) { | ||
throw e; | ||
} | ||
|
||
const err = new Error(); | ||
throw err; | ||
|
||
function err() { | ||
return new Error(); | ||
} | ||
throw err(); | ||
|
||
const foo = { | ||
bar: new Error(), | ||
}; | ||
throw foo.bar; | ||
|
||
class CustomError extends Error { | ||
// ... | ||
} | ||
throw new CustomError(); | ||
``` | ||
|
||
</TabItem> | ||
</Tabs> | ||
|
||
## Options | ||
|
||
This rule adds the following options: | ||
|
||
```ts | ||
interface Options { | ||
/** | ||
* Whether to always allow throwing values typed as `any`. | ||
*/ | ||
allowThrowingAny?: boolean; | ||
|
||
/** | ||
* Whether to always allow throwing values typed as `unknown`. | ||
*/ | ||
allowThrowingUnknown?: boolean; | ||
} | ||
|
||
const defaultOptions: Options = { | ||
allowThrowingAny: false, | ||
allowThrowingUnknown: false, | ||
}; | ||
``` | ||
|
||
{/* Intentionally Omitted: When Not To Use It */} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.