Skip to content

Commit 7bd8687

Browse files
lo1tumasindresorhusfisker
authored
Allow descriptive error names by default in catch-error-name (#581)
Co-authored-by: Sindre Sorhus <[email protected]> Co-authored-by: fisker Cheung <[email protected]>
1 parent 761a3be commit 7bd8687

File tree

3 files changed

+97
-24
lines changed

3 files changed

+97
-24
lines changed

docs/rules/catch-error-name.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Enforce a specific parameter name in catch clauses
22

3-
Applies to both `try/catch` clauses and `promise.catch(...)` handlers.
3+
Applies to both `try/catch` clauses and `promise.catch()` handlers.
44

55
The desired name is configurable, but defaults to `error`.
66

77
This rule is fixable unless the reported code was destructuring an error.
88

9-
109
## Fail
1110

1211
```js
@@ -21,7 +20,6 @@ try {
2120
somePromise.catch(e => {})
2221
```
2322

24-
2523
## Pass
2624

2725
```js
@@ -74,7 +72,6 @@ somePromise.catch(_ => {
7472
});
7573
```
7674

77-
7875
## Options
7976

8077
### name
@@ -101,7 +98,7 @@ You can set the `name` option like this:
10198
]
10299
```
103100

104-
This option lets you specify a regex pattern for matches to ignore. The default is `^_$`.
101+
This option lets you specify a regex pattern for matches to ignore. The default allows `_` and descriptive names like `networkError`.
105102

106103
With `^unicorn$`, this would fail:
107104

@@ -122,3 +119,7 @@ try {
122119
//
123120
}
124121
```
122+
123+
## Tip
124+
125+
In order to avoid shadowing in nested catch clauses, the auto-fix rule appends underscores to the identifier name. Since this might be hard to read, the default setting for `caughtErrorsIgnorePattern` allows the use of descriptive names instead, for example, `fsError` or `authError`.

rules/catch-error-name.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const create = context => {
3434

3535
const options = {
3636
name: 'error',
37-
caughtErrorsIgnorePattern: '^_$',
37+
caughtErrorsIgnorePattern: /^_$|^[\dA-Za-z]+(e|E)rror$/.source,
3838
...context.options[0]
3939
};
4040

test/catch-error-name.js

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,18 @@ ruleTester.run('catch-error-name', rule, {
3434
}
3535
}
3636
`),
37-
testCase(outdent`
37+
testCase(
38+
outdent`
3839
const handleError = err => {
3940
try {
4041
doSomething();
4142
} catch (err_) {
4243
console.log(err_);
4344
}
4445
}
45-
`, 'err'),
46+
`,
47+
'err'
48+
),
4649
testCase(outdent`
4750
const handleError = error => {
4851
const error_ = new Error('🦄');
@@ -60,11 +63,14 @@ ruleTester.run('catch-error-name', rule, {
6063
obj.catch(error_ => { });
6164
}
6265
`),
63-
testCase(outdent`
66+
testCase(
67+
outdent`
6468
const handleError = err => {
6569
obj.catch(err_ => { });
6670
}
67-
`, 'err'),
71+
`,
72+
'err'
73+
),
6874
testCase(outdent`
6975
const handleError = error => {
7076
const error_ = new Error('foo bar');
@@ -91,11 +97,15 @@ ruleTester.run('catch-error-name', rule, {
9197
testCase('obj.catch((_) => {})'),
9298
testCase('obj.catch((_) => { console.log(foo); })'),
9399
testCase('obj.catch(err => {})', 'err'),
94-
testCase('obj.catch(outerError => { return obj2.catch(innerError => {}) })'),
100+
testCase(
101+
'obj.catch(outerError => { return obj2.catch(innerError => {}) })'
102+
),
95103
testCase('obj.catch(function (error) {})'),
96104
testCase('obj.catch(function () {})'),
97105
testCase('obj.catch(function (err) {})', 'err'),
98-
testCase('obj.catch(function (outerError) { return obj2.catch(function (innerError) {}) })'),
106+
testCase(
107+
'obj.catch(function (outerError) { return obj2.catch(function (innerError) {}) })'
108+
),
99109
testCase('obj.catch()'),
100110
testCase('obj.catch(_ => { console.log(_); })'),
101111
testCase('obj.catch(function (_) { console.log(_); })'),
@@ -129,21 +139,76 @@ ruleTester.run('catch-error-name', rule, {
129139
} catch {
130140
console.log('failed');
131141
}
132-
`)
142+
`),
143+
testCase('try {} catch (descriptiveError) {}'),
144+
testCase('try {} catch (descriptiveerror) {}')
133145
],
134146

135147
invalid: [
136-
testCase('try {} catch (err) { console.log(err) }', null, true, 'try {} catch (error) { console.log(error) }'),
137-
testCase('try {} catch (error) { console.log(error) }', 'err', true, 'try {} catch (err) { console.log(err) }'),
148+
testCase(
149+
'try {} catch (err) { console.log(err) }',
150+
null,
151+
true,
152+
'try {} catch (error) { console.log(error) }'
153+
),
154+
testCase(
155+
'try {} catch (error) { console.log(error) }',
156+
'err',
157+
true,
158+
'try {} catch (err) { console.log(err) }'
159+
),
138160
testCase('try {} catch ({message}) {}', null, true),
139-
testCase('try {} catch (outerError) {}', null, true, 'try {} catch (error) {}'),
140-
testCase('try {} catch (innerError) {}', null, true, 'try {} catch (error) {}'),
161+
{
162+
code: 'try {} catch (outerError) {}',
163+
output: 'try {} catch (error) {}',
164+
errors: [
165+
{
166+
ruleId: 'catch-error-message',
167+
message: 'The catch parameter should be named `error`.'
168+
}
169+
],
170+
options: [
171+
{
172+
caughtErrorsIgnorePattern: '^_$'
173+
}
174+
]
175+
},
176+
{
177+
code: 'try {} catch (innerError) {}',
178+
output: 'try {} catch (error) {}',
179+
errors: [
180+
{
181+
ruleId: 'catch-error-name',
182+
message: 'The catch parameter should be named `error`.'
183+
}
184+
],
185+
options: [
186+
{
187+
caughtErrorsIgnorePattern: '^_$'
188+
}
189+
]
190+
},
141191
testCase('obj.catch(err => err)', null, true, 'obj.catch(error => error)'),
142-
testCase('obj.catch(error => error.stack)', 'err', true, 'obj.catch(err => err.stack)'),
192+
testCase(
193+
'obj.catch(error => error.stack)',
194+
'err',
195+
true,
196+
'obj.catch(err => err.stack)'
197+
),
143198
testCase('obj.catch(({message}) => {})', null, true),
144-
testCase('obj.catch(function (err) { console.log(err) })', null, true, 'obj.catch(function (error) { console.log(error) })'),
199+
testCase(
200+
'obj.catch(function (err) { console.log(err) })',
201+
null,
202+
true,
203+
'obj.catch(function (error) { console.log(error) })'
204+
),
145205
testCase('obj.catch(function ({message}) {})', null, true),
146-
testCase('obj.catch(function (error) { console.log(error) })', 'err', true, 'obj.catch(function (err) { console.log(err) })'),
206+
testCase(
207+
'obj.catch(function (error) { console.log(error) })',
208+
'err',
209+
true,
210+
'obj.catch(function (err) { console.log(err) })'
211+
),
147212
// Failing tests for #107
148213
// testCase(outdent`
149214
// foo.then(() => {
@@ -205,6 +270,11 @@ ruleTester.run('catch-error-name', rule, {
205270
ruleId: 'catch-error-name',
206271
message: 'The catch parameter should be named `error_`.'
207272
}
273+
],
274+
options: [
275+
{
276+
caughtErrorsIgnorePattern: '^_$'
277+
}
208278
]
209279
},
210280
{
@@ -295,10 +365,7 @@ ruleTester.run('catch-error-name', rule, {
295365
obj.catch(error => {});
296366
obj.catch(error => {});
297367
`,
298-
errors: [
299-
{ruleId: 'catch-error-name'},
300-
{ruleId: 'catch-error-name'}
301-
]
368+
errors: [{ruleId: 'catch-error-name'}, {ruleId: 'catch-error-name'}]
302369
},
303370
{
304371
code: 'try {} catch (_error) {}',
@@ -333,6 +400,11 @@ ruleTester.run('catch-error-name', rule, {
333400
ruleId: 'catch-error-message',
334401
message: 'The catch parameter should be named `error`.'
335402
}
403+
],
404+
options: [
405+
{
406+
caughtErrorsIgnorePattern: '^_$'
407+
}
336408
]
337409
}
338410
]

0 commit comments

Comments
 (0)