# catch-error-name πŸ“ Enforce a specific parameter name in catch clauses. πŸ’ΌπŸš« This rule is enabled in the βœ… `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). This rule is _disabled_ in the β˜‘οΈ `unopinionated` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config). πŸ”§ This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). Enforces a consistent name for caught errors across: - `try/catch` clauses - `.catch(…)` Promise handlers - `.then(…, rejectionHandler)` Promise handlers Using one recognizable name makes error handlers easier to scan and avoids renaming the same concept across callback styles. The desired parameter name is [configurable](#name), but defaults to `error`. The following names are ignored: - `_`, but only if the error is not used. - Descriptive names, for example, `fsError` or `authError`. - Names matching [`options.ignore`](#ignore). ## Examples ```js // ❌ try {} catch (badName) {} // βœ… try {} catch (error) {} ``` ```js // ❌ promise.catch(badName => {}); // βœ… promise.catch(error => {}); ``` ```js // ❌ promise.then(undefined, badName => {}); // βœ… promise.then(undefined, error => {}); ``` ```js // ❌ try {} catch (_) { console.log(_); } // βœ… // Underscore is allowed when the error is not used try {} catch (_) { // error is not used } ``` ```js // βœ… // Descriptive name is allowed try {} catch (fsError) {} ``` ```js // βœ… // `error_` is allowed because of shadowed variables try {} catch (error_) { const error = new Error('πŸ¦„'); } ``` ## Options ### name Type: `string`\ Default: `'error'` You can set the `name` option like this: ```js 'unicorn/catch-error-name': [ 'error', { name: 'exception', }, ] ``` ### ignore Type: `Array`\ Default: `[]` This option lets you specify a regex pattern for matches to ignore. When a string is given, it's interpreted as a regular expression inside a string. ```js 'unicorn/catch-error-name': [ 'error', { ignore: [ '^error\\d*$', /^ignore/i, ], }, ] ``` With `^unicorn$`, this would fail: ```js try {} catch (pony) {} ``` And this would pass: ```js try {} catch (unicorn) {} ``` ## Tip In order to avoid shadowing in nested catch clauses, the auto-fix rule appends underscores to the identifier name.