Skip to main content

throw-error

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

โš ๏ธ 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: R074

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.