Skip to main content

throw-error

Disallow throwing or rejecting values that are not Error-like.

Deprecated

Enable both replacements to preserve coverage for direct throw statements and Promise rejection values. Their option shapes differ from this combined rule.

⚠️ This rule requires type information to run.

Targeted pattern scope​

This rule checks:

  • ThrowStatement arguments,
  • Promise.reject(...) calls,
  • reject(...) calls inside new Promise((resolve, reject) => ...) executors.

What this rule reports​

This rule reports values that are not Error-like when thrown or used as Promise rejection values.

It allows:

  • Error and DOMException values,
  • values that could be any or unknown (to avoid unsafe assumptions during static analysis).

Why this rule exists​

Throwing primitives or arbitrary objects makes error handling inconsistent and often forces fragile downstream narrowing logic. Enforcing Error-like values improves reliability and observability.

❌ Incorrect​

throw "kaboom";
Promise.reject("kaboom");
new Promise((resolve, reject) => reject("kaboom"));

✅ Correct​

throw new Error("kaboom");
throw new DOMException("kaboom");
Promise.reject(new Error("kaboom"));
new Promise((resolve, reject) => reject(new Error("kaboom")));

Behavior and migration notes​

This rule has no options.

Additional examples​

declare const maybeUnknown: unknown;
throw maybeUnknown;
// ✅ allowed (unknown is intentionally permitted)

const payload = { code: "E_FAIL" };
throw payload;
// ❌ reported (not Error-like)

ESLint flat config example​

import etcMisc from "eslint-plugin-etc-misc";

export default [
{
plugins: { "etc-misc": etcMisc },
rules: {
"etc-misc/throw-error": "error",
},
},
];

When not to use it​

Disable this rule if your codebase intentionally uses non-Error throw/reject values and has established handling utilities for that pattern.

Package documentation​

Rule catalog ID: R100

Further reading​

Adoption resources​

  • Start at warning level in CI, then move to error after cleanup.
  • Use focused codemods/autofix batches per package or directory.