redline
MediumError handlingthrow-literal

Throws a non-Error

Throws a non-Error. Throws a throw "… value instead of an Error. There will be no stack trace.

This is how the comment reads inline, under the flagged line.

Why it matters

Throwing a string or object loses the stack trace and breaks instanceof Error checks in handlers.

What to do

throw new Error("…"), or a subclass with a cause.

Examples

Flagged
throw "user not found";
Better
throw new NotFoundError("user not found");

What it catches

It reads a pattern on each line, aware of strings and comments. Languages: TypeScript and JavaScript. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.

Flagged

throw "bad";
throw { code: 1 };

Left alone

throw new Error("bad");

Research

Throwing a string or plain object. There is no stack trace, and `instanceof Error` checks fail.

Turn it off or change it

In a config file for the CLI, or in the API's rules field:

{
  "rules": {
    "disable": [
      "throw-literal"
    ],
    "severity": {
      "throw-literal": "low"
    }
  }
}

Read the source or report a false positive.