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.

Subclassing Error has several common pitfalls: forgetting to set this.name, assigning this.message redundantly when the value is already passed to super(), and using this.constructor.name instead of a string literal (which breaks after minification). This rule enforces the one correct pattern for extending any class whose name ends in Error.
Recommended
Fixable🔧 Auto-fix
Suggestions

Examples

// Redundant `this.message` — already set by super()
class CustomError extends Error {
  constructor(message) {
    super(message);
    this.message = message;
    this.name = 'CustomError';
  }
}

// Missing super() argument — should pass message to super()
class CustomError extends Error {
  constructor(message) {
    super();
    this.message = message;
    this.name = 'CustomError';
  }
}

// Missing `this.name` — error shows as [Error: …] instead of [CustomError: …]
class CustomError extends Error {
  constructor(message) {
    super(message);
  }
}

// Breaks after minification — use a string literal instead
class CustomError extends Error {
  constructor(message) {
    super(message);
    this.name = this.constructor.name;
  }
}

// Wrong name value
class CustomError extends Error {
  constructor(message) {
    super(message);
    this.name = 'MyError';
  }
}

// Class name must start with uppercase and end with "Error"
class foo extends Error {
  constructor(message) {
    super(message);
    this.name = 'foo';
  }
}

Rules enforced

  1. The class name must be PascalCase and end with Error (for example, FooError, not foo).
  2. The constructor must call super().
  3. The error message must be passed to super(), not assigned via this.message.
  4. this.name must be set to a string literal matching the class name — not this.constructor.name.
This rule works with any superclass whose name ends in Error, not just the built-in Error. For example, class MyError extends TypeError is also checked.

Build docs developers (and LLMs) love