unicorn/error-message
Require a message when creating a built-in error
An error without a message shows up in a log as Error and a stack trace. Anyone who reads it needs to open the source to find out what went wrong.
The rule checks Error, TypeError, RangeError and the other built-in errors, called with or without new. It reports a missing message, an empty string, and an array or an object where the message goes. The message is the second argument of AggregateError and the third of SuppressedError.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/error-message" = "error" # or "warn"Examples#
example.js3 problems
function parsePort(value) {
if (value === undefined) throw new Error()Pass a message to the Error constructor.;
if (value === "") throw new TypeError(""Error message should not be an empty string.);
if (value > 65_535) throw new RangeError({ value }Error message should be a string.);
return Number(value);
}example.js1 problem
throw new AggregateError(errors)Pass a message to the AggregateError constructor.;example.jsNo problems
function parsePort(value) {
if (value === undefined) throw new Error("The port is missing");
if (value === "") throw new TypeError("The port is empty");
if (value > 65_535) throw new RangeError(`Port ${value} is out of range`);
return Number(value);
}example.jsNo problems
throw new AggregateError(errors, "All mirrors failed");A variable is accepted as is, because the rule doesn't know what it holds.
example.jsNo problems
throw new Error(message);