Skip to content

Latest commit

 

History

History
110 lines (84 loc) · 1.77 KB

custom-error-definition.md

File metadata and controls

110 lines (84 loc) · 1.77 KB

Enforce correct Error subclassing

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

This rule is fixable.

Fail

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

The this.message assignment is useless as it's already set via the super() call.

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

Pass the error message to super() instead of setting this.message.

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);
		this.name = this.constructor.name;
	}
}

Use a string literal to set the name property as it will not change after minifying.

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

The name property should be set to the class name.

class foo extends Error {
	constructor(message) {
		super(message);
		this.name = 'foo';
	}
}

The class name is invalid. It should be capitalized and end with Error. In this case it should be FooError.

Pass

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';
	}
}