unicorn/catch-error-name Style
What it does
This rule enforces consistent and descriptive naming for error variables in catch
statements, preventing the use of vague names like badName
or _
when the error is used.
Why is this bad?
Using non-descriptive names like badName
or _
makes the code harder to read and understand, especially when debugging. It's important to use clear, consistent names to represent errors.
Examples
Examples of incorrect code for this rule:
try {
} catch (badName) {}
// `_` is not allowed if it's used
try {
} catch (_) {
console.log(_);
}
promise.catch((badName) => {});
promise.then(undefined, (badName) => {});
Examples of correct code for this rule:
try {
} catch (error) {}
// `_` is allowed if it's not used
try {
} catch (_) {
console.log(123);
}
promise.catch((error) => {});
promise.then(undefined, (error) => {});
Options
name
{ type: string, default: "error" }
The name to use for error variables in catch
blocks. You can customize it to something other than 'error'
(e.g., 'exception'
).
Example:
"unicorn/catch-error-name": [
"error",
{ "name": "exception" }
]
ignore
{ type: Array<string | RegExp>, default: [] }
A list of patterns to ignore when checking catch
variable names. The pattern can be a string or regular expression.
Example:
"unicorn/catch-error-name": [
"error",
{
"ignore": [
"^error\\d*$"
]
}
]
How to use
To enable this rule in the CLI or using the config file, you can use:
oxlint --deny unicorn/catch-error-name
{
"rules": {
"unicorn/catch-error-name": "error"
}
}