---
url: /docs/guide/usage/linter/rules/unicorn/custom-error-definition.md
---

### What it does

Enforces the only valid way of Error subclassing. It works with any super class that ends in Error.

### Why is this bad?

Incorrectly defined custom errors can lead to unexpected behavior when
catching and identifying errors. Missing `super()` calls, wrong `name`
property values, or non-standard class names make error handling unreliable.

### Examples

Examples of **incorrect** code for this rule:

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

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

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

class CustomError extends Error {
  constructor(message) {
    super(message);
    // Use a string literal to set the `name` property as it will not change after minifying.
    this.name = this.constructor.name;
  }
}

class CustomError extends Error {
  constructor(message) {
    super(message);
    // The `name` property should be set to the class name.
    this.name = "MyError";
  }
}
```

Examples of **correct** code for this rule:

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

class CustomError extends Error {
  constructor() {
    super("My custom error");
    this.name = "CustomError";
  }
}

class CustomError extends TypeError {
  constructor() {
    super();
    this.name = "CustomError";
  }
}

class CustomError extends Error {
  name = "CustomError";
}
```

## How to use

## Version

This rule was added in v1.57.0.

## References
