๐ Enforce a specific parameter name in catch clauses.
๐ผ๐ซ This rule is enabled in the โ
recommended config. This rule is disabled in the โ๏ธ unopinionated config.
๐ง This rule is automatically fixable by the --fix CLI option.
Applies to
try/catchclauses handlerspromise.catch(โฆ)handlerspromise.then(onFulfilled, โฆ)handlers
The desired name is configurable, but defaults to error.
The following names are ignored:
_, but only if the error is not used.- Descriptive names, for example,
fsErrororauthError. - Names matching
options.ignore.
// โ
try {} catch (badName) {}
// โ
try {} catch (error) {}// โ
promise.catch(badName => {});
// โ
promise.catch(error => {});// โ
promise.then(undefined, badName => {});
// โ
promise.then(undefined, error => {});// โ
try {} catch (_) {
console.log(_);
}
// โ
try {} catch (_) {
console.log(foo);
}// โ
// Descriptive name is allowed
try {} catch (fsError) {}// โ
// `error_` is allowed because of shadowed variables
try {} catch (error_) {
const error = new Error('๐ฆ');
}Type: string
Default: 'error'
You can set the name option like this:
"unicorn/catch-error-name": [
"error",
{
"name": "exception"
}
]Type: Array<string | RegExp>
Default: []
This option lets you specify a regex pattern for matches to ignore.
When a string is given, it's interpreted as a regular expressions inside a string. Needed for ESLint config in JSON.
"unicorn/catch-error-name": [
"error",
{
"ignore": [
"^error\\d*$",
/^ignore/i
]
}
]With ^unicorn$, this would fail:
try {} catch (pony) {}And this would pass:
try {} catch (unicorn) {}In order to avoid shadowing in nested catch clauses, the auto-fix rule appends underscores to the identifier name.