# custom-error-definition

Rule `unicorn/custom-error-definition`. Enforce correct `Error` subclassing.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 2.1M weekly downloads of presets that enable it
- Trend: +151.7% last quarter
- Rank: #1510 by reach, #14 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/custom-error-definition` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/custom-error-definition.md) |
| Oxlint | `unicorn/custom-error-definition` | no | no | plugin `unicorn`, category style | [docs](https://oxc.rs/docs/guide/usage/linter/rules/unicorn/custom-error-definition.html) |
| Biome | `nursery/useCustomErrorDefinition` | no | no | group nursery, since 2.6.10 | [docs](https://biomejs.dev/linter/rules/use-custom-error-definition/) |

## Code examples

### Incorrect: the rule reports this

```js
class CustomError extends Error {
  constructor(message, options) {
    super(message, options);
    // The `this.message` assignment is useless as it's already set via the `super()` call.
    this.message = message;
    this.name = 'CustomError';
  }
}
```

```js
class CustomError extends Error {
  constructor(message, options) {
    super(undefined, options);
    // Pass the error message to `super()` instead of setting `this.message`.
    this.message = message;
    this.name = 'CustomError';
  }
}
```

```js
class CustomError extends Error {
  constructor(message, options) {
    super(message, options);
    // No `name` property set. The name property is needed so the
    // error shows up as `[CustomError: foo]` and not `[Error: foo]`.
  }
}
```

### Correct: the rule accepts this

```js
class UserNotFoundError extends GraphQLError {
  constructor(userId) {
    super(`User "${userId}" does not exist`, {extensions: {code: 'NOT_FOUND'}});
    this.name = 'UserNotFoundError';
  }
}
```

```js
class CustomError extends Error {
  constructor(message, options) {
    super(message, options);
    this.name = 'CustomError';
  }
}
```

```js
class FooError extends Error {
  constructor(message, options) {
    super(message, options);
    this.name = 'FooError';
  }
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/custom-error-definition": "error",
    },
  },
];
```

### Oxlint: `.oxlintrc.json`

```json
{
  "rules": {
    "unicorn/custom-error-definition": "error"
  }
}
```

### Biome: `biome.json`

```json
{
  "linter": {
    "rules": {
      "nursery": {
        "useCustomErrorDefinition": "error"
      }
    }
  }
}
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Ultracite for Oxlint](https://lintradar.dev/presets/ultracite-oxlint.md) | Oxlint | `ultracite` | 475 | 2.1M | +151.7% |

## Related rules

- [unicorn/no-array-for-each](https://lintradar.dev/rules/unicorn/no-array-for-each.md): Forbids the use of `Array#forEach` in favor of a for loop.
- [unicorn/prefer-import-meta-properties](https://lintradar.dev/rules/unicorn/prefer-import-meta-properties.md): Prefer `import.meta.{dirname,filename}` over legacy techniques for getting file paths.
- [unicorn/require-post-message-target-origin](https://lintradar.dev/rules/unicorn/require-post-message-target-origin.md): Enforce using the `targetOrigin` argument with `window.postMessage()`.
- [unicorn/no-invalid-file-input-accept](https://lintradar.dev/rules/unicorn/no-invalid-file-input-accept.md): Disallow invalid `accept` values on file inputs.
- [unicorn/no-empty-link-text](https://lintradar.dev/rules/unicorn/no-empty-link-text.md): Disallow empty link text in Markdown.
- [unicorn/no-javascript-url](https://lintradar.dev/rules/unicorn/no-javascript-url.md): Disallow `javascript:` URLs in Markdown.

---

Web page: https://lintradar.dev/rules/unicorn/custom-error-definition. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
