Linter · Rules
unicorn/custom-error-definition
Enforce that subclasses of Error set name and pass the message to super()
A class that extends Error inherits name, so without more work String(new ValidationError("Bad email")) is "Error: Bad email", and a check such as error.name === "ValidationError" never matches.
The rule looks at every named class that extends Error or a class with a name like TypeError or HttpError. It reports:
- a
namethat's missing, or is anything other than the class name written as a string - a constructor that doesn't call
super() this.message = messageinstead ofsuper(message)- a class name that doesn't end in
Erroror doesn't start with a capital letter exports.A = class B extends Error, where the two names differ
Configure
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/custom-error-definition" = "error" # or "warn"
Examples
Reported:
class ValidationError extends Error {}
class ValidationError extends Error {
constructor(message) {
super();
this.message = message;
this.name = this.constructor.name;
}
}
class Validation extends Error {
name = "Validation";
}
Not reported:
class ValidationError extends Error {
constructor(message) {
super(message);
this.name = "ValidationError";
}
}
A class field works too:
class ValidationError extends Error {
name = "ValidationError";
}