useCatchErrorName (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useCatchErrorName - This rule has an unsafe fix.
- The default severity of this rule is information.
- Sources:
- Same as
unicorn/catch-error-name
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useCatchErrorName": "error" } } }}Description
Section titled “Description”Require a specific name for caught errors.
When an error is caught, it is stored in a variable whose name you choose. Using the same name for this variable everywhere makes error handling code easier to read and to search.
The rule checks the variable of:
try...catchstatements;- functions passed to
promise.catch(...); - functions passed as the second argument of
promise.then(..., ...), which handle errors.
The following names are always allowed:
- the configured name, followed by any number of underscores, such as
error_. An underscore avoids a clash with another variable of the same name. - names that end with the configured name, such as
fsErrororparse_error. These names describe what kind of error is stored. _, when the variable is never used.- names listed in the
ignoreoption.
The fix renames the variable and all its uses.
When the configured name is already used by another variable, the fix adds underscores at the end of the name, such as error_.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”try {} catch (err) {}code-block.js:1:15 lint/nursery/useCatchErrorName FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The caught error is named err instead of error.
> 1 │ try {} catch (err) {}
│ ^^^
2 │
ℹ Using the same name for caught errors everywhere makes error handling code easier to read and search.
ℹ Rename the variable to error.
ℹ 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: Rename the variable to error.
1 │ - try·{}·catch·(err)·{}
1 │ + try·{}·catch·(error)·{}
2 2 │
promise.catch((e) => {});code-block.js:1:16 lint/nursery/useCatchErrorName FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The caught error is named e instead of error.
> 1 │ promise.catch((e) => {});
│ ^
2 │
ℹ Using the same name for caught errors everywhere makes error handling code easier to read and search.
ℹ Rename the variable to error.
ℹ 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: Rename the variable to error.
1 │ - promise.catch((e)·=>·{});
1 │ + promise.catch((error)·=>·{});
2 2 │
promise.then(undefined, function (reason) {});code-block.js:1:35 lint/nursery/useCatchErrorName FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The caught error is named reason instead of error.
> 1 │ promise.then(undefined, function (reason) {});
│ ^^^^^^
2 │
ℹ Using the same name for caught errors everywhere makes error handling code easier to read and search.
ℹ Rename the variable to error.
ℹ 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: Rename the variable to error.
1 │ - promise.then(undefined,·function·(reason)·{});
1 │ + promise.then(undefined,·function·(error)·{});
2 2 │
try {} catch (_) { console.log(_);}code-block.js:1:15 lint/nursery/useCatchErrorName FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The caught error is named _ instead of error.
> 1 │ try {} catch (_) {
│ ^
2 │ console.log(_);
3 │ }
ℹ Using the same name for caught errors everywhere makes error handling code easier to read and search.
ℹ Rename the variable to error.
ℹ 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: Rename the variable to error.
1 │ - try·{}·catch·(_)·{
2 │ - ····console.log(_);
1 │ + try·{}·catch·(error)·{
2 │ + ····console.log(error);
3 3 │ }
4 4 │
try {} catch (error) {}
promise.catch((error) => {});
promise.then(undefined, function (error) {});
try {} catch (_) {}
try {} catch (fsError) {}
try {} catch (error_) { const error = new Error("Something went wrong");}Options
Section titled “Options”Default: "error"
The required name of caught errors.
{ "linter": { "rules": { "nursery": { "useCatchErrorName": { "level": "on", "options": { "name": "exception" } } } } }}Invalid
Section titled “Invalid”try {} catch (error) {}code-block.js:1:15 lint/nursery/useCatchErrorName FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The caught error is named error instead of exception.
> 1 │ try {} catch (error) {}
│ ^^^^^
2 │
ℹ Using the same name for caught errors everywhere makes error handling code easier to read and search.
ℹ Rename the variable to exception.
ℹ 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: Rename the variable to exception.
1 │ - try·{}·catch·(error)·{}
1 │ + try·{}·catch·(exception)·{}
2 2 │
try {} catch (exception) {}ignore
Section titled “ignore”Default: []
A list of names that are always allowed, in addition to the configured name.
{ "linter": { "rules": { "nursery": { "useCatchErrorName": { "level": "on", "options": { "ignore": [ "exception" ] } } } } }}try {} catch (exception) {}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.