Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sindresorhus/eslint-plugin-unicorn/llms.txt

Use this file to discover all available pages before exploring further.

Consistent naming for caught errors makes code easier to scan and avoids confusion in nested try/catch blocks. This rule enforces a standard name (error by default) for catch parameters in try/catch clauses, promise.catch() handlers, and the rejection handler of promise.then().
Recommendedrecommended
Fixable🔧 Auto-fix
Suggestions

Applies to

  • try/catch clause handlers
  • promise.catch(…) handlers
  • promise.then(onFulfilled, …) handlers

Examples

try {} catch (badName) {}

promise.catch(badName => {});

promise.then(undefined, badName => {});

try {} catch (_) {
  console.log(_); // `_` is used, so it must be renamed
}

Configuration

eslint.config.js
export default [
  {
    rules: {
      'unicorn/catch-error-name': ['error', { name: 'error', ignore: [] }],
    },
  },
];

name

Type: string — Default: 'error' The expected name for the caught error parameter. Change this if your codebase uses a different convention.
eslint.config.js
'unicorn/catch-error-name': ['error', { name: 'exception' }]

ignore

Type: Array<string | RegExp> — Default: [] A list of patterns. Any catch parameter whose name matches one of these patterns is ignored. Strings are interpreted as regular expressions.
eslint.config.js
'unicorn/catch-error-name': [
  'error',
  {
    ignore: [
      '^error\\d*$',
      /^ignore/i,
    ],
  },
]

Ignored names

The following names are always allowed without configuration:
  • _ — but only when the error variable is never referenced.
  • Names that end with error or Error (for example, fsError, authError).
  • Names that match a pattern in the ignore option.
When the auto-fix would create a name conflict with an existing variable in scope, it appends underscores (for example, error_) to avoid shadowing.

Build docs developers (and LLMs) love