# 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`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"unicorn/error-message" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 3} */}
```js
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);
}
```

{/* bad */}
```js
throw new AggregateError(errors);
```

Not reported:

{/* good */}
```js
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);
}
```

{/* good */}
```js
throw new AggregateError(errors, "All mirrors failed");
```

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

{/* good */}
```js
throw new Error(message);
```
