Promise.reject Without an Error
ID |
javascript.prefer_promise_reject_errors |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
JavaScript |
Tags |
best-practice, reliability |
Description
Reports Promise.reject(value) calls whose argument is not an Error instance — a string, number, null, an object literal, etc. Rejecting with a non-Error value loses the stack trace and breaks the standard instanceof Error discrimination in catch handlers.
Rationale
-
Without a stack trace, the catch handler has no clue where the rejection originated.
-
instanceof Errordiscrimination — common in centralized error middleware — silently fails for non-Error rejections. -
Unhandled-rejection logs become much less informative (often just
[object Object]).
This is the Promise sibling of javascript.no_throw_literal.
// Bad
return Promise.reject("not found");
return Promise.reject({ code: 404 });
Remediation
Reject with a real Error (or a domain-specific subclass).
// Good
return Promise.reject(new NotFoundError("not found", { code: 404 }));
return Promise.reject(new Error("not found"));