AphrodyBun GitHub

Runtime docs · Linter · Rules

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

Reported:

function parsePort(value) {
  if (value === undefined) throw new Error();
  if (value === "") throw new TypeError("");
  if (value > 65_535) throw new RangeError({ value });
  return Number(value);
}
throw new AggregateError(errors);

Not reported:

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);
}
throw new AggregateError(errors, "All mirrors failed");

A variable is accepted as is, because the rule doesn't know what it holds.

throw new Error(message);