prefer-promise-reject-errors

Require an Error object as the reason of a promise rejection

A promise rejected with a string or a plain object has no stack trace, so you can't see where it failed. The code that catches it and reads error.message gets undefined.

The rule checks Promise.reject() and the reject function of new Promise((resolve, reject) => ...). It reports a reason that can't be an Error: a literal, an object literal, undefined, or nothing. A variable or a call's result isn't reported, because the rule can't tell what it holds.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"prefer-promise-reject-errors" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"prefer-promise-reject-errors" = ["error", {
  allowEmptyReject = false,
}]

Examples#

example.js2 problems
Promise.reject("User not found");

new Promise((resolve, reject) => {
  reject({ code: 404 });
});
example.jsNo problems
Promise.reject(new Error("User not found"));

new Promise((resolve, reject) => {
  reject(new RangeError("Page out of range"));
});

Promise.reject(reason);

Options#

OptionDefaultWhat it does
allowEmptyRejectfalseAllow a rejection with no argument.
example.jswith { "allowEmptyReject": true }No problems
Promise.reject();

Writing out undefined is still reported:

example.jswith { "allowEmptyReject": true }1 problem
Promise.reject(undefined);